I have setup cronjob for root user in ubuntu environment as follows by typing crontab -e
34 11 * * * sh /srv/www/live/CronJobs/daily.sh
0 08 * * 2 sh /srv/www/live/CronJobs/weekly.sh
0 08 1 * * sh /srv/www/live/CronJobs/monthly.sh
But the cronjon do not run. I have tried checking if the cronjob is running using
pgrep cron
and that gives process id 3033.The shell scrip is calls python file and is used to send email. Running the python file is ok. There's no error in it but the cron doesn't run. The daily.sh file has following code in it.
python /srv/www/live/CronJobs/daily.py
python /srv/www/live/CronJobs/notification_email.py
python /srv/www/live/CronJobs/log_kpi.py
Finally I found the solution. Following is the solution:-
Never use relative path in python scripts to be executed via crontab. I did something like this instead:-
Never supress the crontab code instead use mailserver and check the mail for the user. That gives clearer insights of what is going.
For me, the solution was that the file cron was trying to run was in an encrypted directory, more specifcically a user diretory on /home/. Although the crontab was configured as root, because the script being run exisited in an encrypted user directory in /home/ cron could only read this directory when the user was actually logged in. To see if the directory is encrypted check if this directory exists:
if so then you have an encrypted home directory.
The fix for me was to move the script in to a non=encrypted directory and everythig worked fine.
WTF?! My cronjob doesn't run?!
Here's a checklist guide to debug not running cronjobs:
ps ax | grep cron
and look for cron.service cron start
orservice cron restart
* * * * * /bin/echo "cron works" >> /tmp/file
/tmp
which does not currently exist should always be writable./var/log/cron.log
or/var/log/messages
for errors.grep CRON /var/log/syslog
/var/log/cron
chmod +x /var/www/app/cron/do-stuff.php
30 1 * * * command > /dev/null 2>&1
Still not working? Yikes!
/etc/default/cron
EXTRA_OPTS="-L 2"
service cron restart
tail -f /var/log/syslog
to see the scripts executed/etc/rsyslog.d/50-default.conf
cron.crit /var/log/cron.log
sudo /etc/init.d/rsyslog reload
/var/log/cron.log
and look for detailed error outputCronjob Syntax
This syntax is only correct for the
root
user. Regular usercrontab
syntax doesn't have the User field (regular users aren't allowed to run code as any other user);Crontab Commands
crontab -l
crontab -e
, for a specific user:crontab -e -u agentsmith
crontab -r
I experienced same problem where crons are not running. We fixed by changing permissions and owner by Crons made root owner as we had mentioned in crontab AND Cronjobs 644 permission given
I want to add 2 points that I learned:
Refs:
I've found another reason for user's crontab not running: the hostname is not present on the hosts file:
Now the hosts file:
This is on a Ubuntu 14.04.3 LTS, the way to fix it is adding the hostname to the hosts file so it resembles something like this: