Seems Cron job working even after Stopped - php

one of my cronjobs send an email daily
35 6 * * * cd $EZPUBLISHROOT && $PHP runcronjobs.php -q 2>&1
I stopped cron using
sudo service cron stop
when i check the cron status
sudo service cron status
it is cron stop/waiting
the problem is i stopped my crons yesterday , but today also the email was sent again
am i doing this wrong
1.how to stop all cron jobs properly
2.how to view last ran cronjob list .
i tried to get last ran cronob details by
grep cron /var/log/syslog
but it is empty , but when i go to the syslog file i can see the word CRON in some places .
Aug 12 09:10:01 64177 CRON[6388]: (root) CMD (cd /var/www && /usr/bin/php runcronjobs.p......
also when i tries typing
ps -ax|grep cron
5696 pts/3 S+ 0:00 grep --color=auto cron
13011 ? Ss 0:00 cron l
is it possble to run crons after i stopped the cron jobs ??
Thanks in advnace

The proper way to do it is to put every job or every group of jobs in their own 'namespace' / file:
/etc/cron.d/production-job-mail-on-delivery
/etc/cron.d/production-aggregate-account-data
/etc/cron.d/devel-job-mail-on-delivery
/etc/cron.d/system-cleanup-sundy
/etc/cron.d/common-mailings
When you want to stop, you just move those files out of the /etc/cron.d/ folder.
You should not stop the cron service and you shouldn't use crontab / /etc/crontab on a bigger system.
Why?
For example, I have 200 cronjobs on a system. /etc/crontab file gets very messy. Some jobs need to be stopped for a week for example, others need to run permanently. Using cron.d folder this is a very simple task.

Normally you would access your crontab with:
crontab -l (lists cronjobs)
and to edit:
crontab -e
to stop a cronjob temporarily you can usually just comment out the first digit:
# 35 6 * * * cd $EZPUBLISHROOT && $PHP runcronjobs.php -q 2>&1
to no longer run a cronjob permanently just delete the particular job. crontab usually restarts itself automatically after you edit it. if not trying using:
sudo restart cron
more information: http://man7.org/linux/man-pages/man1/crontab.1.html

The simple way to do it is commenting the cronjob trough a crontab -e:
#35 6 * * * cd $EZPUBLISHROOT && $PHP runcronjobs.php -q 2>&1
Other than that you should configure the time the frequency of the cronjob to run.
To stop processes you can
ps|aux grep runcronjobs
and after you get a list of the processes runing you can just kill a process using its number
kill *number

Related

Execute the script using crontab

I need to execute a php script once in a day.
I followed these steps to make the script run.
crontab -e
30 17 * * * /usr/bin/php /var/www/html/file.php >> /var/log/1.log >2&1
Its not working.There is nothing inside 1.log(1.log is the file which i have created)
I checked the log file using
grep CRON /var/log/syslog
CMD redirected to /usr/bin/php /var/www/html/file.php >> /var/log/1.log >2&1
MTA was not installed ,discarded the output.
but i do not want a mail to be sent so i do not want to install MTA. I just want cronjob to be run.
Any help would be appreciated

Cron is not executing files

I have a php file to execute every minute but when I am running it on command line it executes as expected, but when set to cron job it does not run. Below mentioned are things which I have checked:
cron service
service crond status
crond (pid 183872) is running...
Checked logs of cron execution :
May 21 00:27:01 spartans CROND[194123]: (root) CMD (/usr/bin/php /home/dev/my.php)
May 21 00:27:01 e2e CROND[194122]: (root) MAIL (mailed 84 bytes of output but got status 0x004b#012)
CronJob list in crontab -e
*/1 * * * * /usr/bin/php /home/dev/my.php
My request is to help me in finding error where things are getting wrong. I have started cron service couple of times.

Unix Cron Not Executing

I'm executing a shell script as a cron job every minute like so:
* * * * * /bin/bash /var/www/html/stream.sh
The script contains the following code:
#!/bin/sh
if ps -ef | grep -v grep | grep get_tweets.php ; then
exit 0
else
nohup php /var/www/html/streaming/db/get_tweets.php > /dev/null &
exit 0
fi
I'm also running another shell script as a cron, the only difference between the two is "get_tweets" is replaced with "parse_tweets_keyword" and cron is executed like so:
* * * * * /bin/bash /var/www/html/process-check.sh
Problem is, whilst the latter cron works perfectly fine, the first doesn't seem to run the script successfully, however when I run the command:
nohup php /var/www/html/streaming/db/get_tweets.php > /dev/null &
The script runs perfectly, so i'm not entirely sure what the problem is. The permissions for all files are correct and executable and i'm running the crons as root under crontab and both of the PHP script that i'm attempting to execute are running as background processes.
If anyone can help or knows the issue, would be greatly appreciated. I'm also open to better ways of running the PHP script perhaps as a single line in crontab rather than running the shell script to run the PHP script via cron.
So it seems this was the answer to my question:
#!/bin/sh
cd /var/www/html/streaming/db
if ps ax | grep -v grep | grep get_tweets.php ;
then
exit 0
else
nohup php ./get_tweets.php > /dev/null &
exit 0
fi
It would appear even running as root get_tweets.php would not execute without first changing the directory prior to actually running the command. As per the second line:
cd /var/www/html/streaming/db
Although I'm not entirely sure why it wouldn't run, as parse_tweets_keyword.php ran perfectly using the first bash script posted in my question.

Elastic Beanstalk - Cron job is running but not executing

This is my very first time running a cron job on Elastic Beanstalk (EB). After deploying my code, it seems the cron job is created and running but the PHP script is not executing correctly. Here's my set-up.
In my .ebextensions folder I have a file called 01run.config.
container_commands:
01_remove_old_cron_jobs:
command: "crontab -r || exit 0"
02_cronjobs:
command: "cat .ebextensions/cron_jobs.txt > /etc/cron.d/cron_job && chmod 644 /etc/cron.d/cron_job"
leader_only: true
In my .ebextensions folder I also have a cron_jobs.txt file. Please note that I have an line break at the end of this file as instructed by another stackoverflow post. In my example below I am running the command as ec2-user but I also tried root.
* * * * * ec2-user /usr/bin/php -q /var/app/current/tests/cron.php
After deploying my code, I can see that the file /etc/cron.d/cron_job has been created. I can also see the cron job running every minute when I run sudo tail /var/log/cron.
[ec2-user#ip-xxx-xxx-xxx-xxx ~]$ sudo tail /var/log/cron
Apr 13 12:54:53 ip-xxx-xxx-xxx-xxx crontab[26093]: (root) DELETE (root)
Apr 13 12:55:01 ip-xxx-xxx-xxx-xxx crond[1230]: (*system*) RELOAD (/etc/cron.d/cron_job)
Apr 13 12:55:01 ip-xxx-xxx-xxx-xxx CROND[26128]: (ec2-user) CMD (/usr/bin/php -q /var/app/current/tests/cron.php)
Apr 13 12:56:01 ip-xxx-xxx-xxx-xxx CROND[26139]: (ec2-user) CMD (/usr/bin/php -q /var/app/current/tests/cron.php)
Within /var/app/current/tests/cron.php I have some code that adds a row to a MySQL database (hosted on RDS). But nothing is being added to the database.
I then tried running the cron command directly through my terminal window:
$ /usr/bin/php -q /var/app/current/tests/cron.php
And it runs without error and adds the record to the database. I am logged in as ec2-user in terminal.
Have I missed something? Or is my cron job code set-up incorrectly?
I had a similar problem with a php script that was trying to access an AWS RDS database. Is your php script getting the database details with $_SERVER['RDS_xxxx']? If so, those RDS_xxxx variables don't exist in the environment when the php script is run by cron.
In order to fix this, I added the variables to the beginning of the cron file:
RDS_HOSTNAME=<my_database_hostname>
RDS_PORT=<my_database_port>
RDS_USERNAME=<my_database_username>
RDS_PASSWORD=<my_database_password>
RDS_DB_NAME=<my_database_name>
* * * * * php /path/to/my/script.php
Login via SSH and check if generated cron job file/etc/cron.d/cron_job have unix line ending i.e. ASCII text not win i.e. ASCII text, with CRLF line terminators.
To check the line ending refer the answer here.
Note: If you have windows line ending then you will have to convert the line ending of file .ebextensions/cron_jobs.txt, for that you can use dos2unix or similar program.
I had a similar problem with my RDS_ variables on AWS, I followed this discussion and it works.
This was my cronjob before:
RDS_HOSTNAME=<my_database_hostname>
RDS_PORT=<my_database_port>
RDS_USERNAME=<my_database_username>
RDS_PASSWORD=<my_database_password>
RDS_DB_NAME=<my_database_name>
* * * * * cd /var/app/current && bin/cake notifications send_push >> /var/tmp/notifications.log 2>&1
And changed to this:
* * * * * . /opt/elasticbeanstalk/support/envvars cd /var/app/current && bin/cake notifications send_push >> /var/tmp/notifications.log 2>&1
And now I can access them like: $_SERVER['RDS_HOSTNAME']

Problem running a small script as cron job

I am problem scheduling and running a script through cron job. I am on linux (ubuntu), it is a VPS.
What I am doing is I have put this line in crontab file that is here: /etc/crontab
I wrote:
*/15 * * * * www-data php /var/www/abs/phpscript.php
I have given 777 to the file and after writing above in crontab , I run command:
crontab crontab
Then after almost some time I got the mail in my /var/mail/username file that says: /bin/sh: root: not found
So I am unable to understand what is the problem.
I also run phpinfo and it shows the third variable as APACHE that probably means that PHP is running as apache module.
Please tell what can be the possible solution.
thanks in advance to every one who will try to solve my problem.
You can try also to run it using "wget -q -O"
or
*/15 * * * * lynx -dump "url" > /dev/null
Wget examples:
*/15 * * * * wget -O /dev/null 'http://www.mydomain.com/document.php?&user=myuser&password=mypass' >/dev/null
If you need to post data you can use
--post-data "login=user&password=pass"
*/15 * * * * wget -O /dev/null 'http://www.mydomain.com/document.php?&user=myuser&password=mypass' --post-data 'foo=bar' >/dev/null
If you edited /etc/crontab, you should re-read the warning at the top of the file:
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
Running crontab(1) on the /etc/crontab file probably contaminated the root user's crontab(5) file (the one stored in /var/spool/cron/crontabs/). I suggest running crontab -e as root to edit the crontab file, and remove all the entries that are identical to the entries from /etc/crontab. (Maybe you just contaminated your own personal crontab(5) -- if crontab -e as root didn't show anything, run crontab -e under your own personal account and see if the system-wide entries were duplicated into your own crontab(5).)
I don't know what file you ran chmod 777 on, but that was probably unnecessary. You really should set your permissions to be as strict as possible to confine the results of malicious attacks or unintentional mistakes.
You are running a crontab as a user, which means you can't specify the user in the cron.
The template you borrowed your example from was for a system (root) cron.
Remove the username and try again.

Categories