WTF?! My cronjob doesn’t run?!
Here’s a checklist guide to debug not running cronjobs:
- Is the Cron daemon running?
- Run
ps ax | grep cron
and look for cron. - Debian:
service cron start
orservice cron restart
- Run
- Is cron working?
* * * * * /bin/echo "cron works" >> /file
- Syntax correct? See below.
- Is the command working standalone?
- Check if the script has an error, by doing a dry run on the CLI
- when testing your command, test as the user whose crontab you are editing, which might not be your login or root
- Can cron run your job?
- Check
/var/log/cron.log
or/var/log/messages
for errors. - Ubuntu:
grep CRON /var/log/syslog
- Redhat:
/var/log/cron
- Check
- Check permissions
- set executable flag on the command:
chmod +x /var/www/app/cron/do-stuff.php
- if you redirect the output of your command to a file, verify you have permission to write to that file/directory
- set executable flag on the command:
- Check paths
- check she-bangs / hashbangs line
- do not rely on environment variables like PATH, as their value will likely not be the same under cron as under an interactive session
- Don’t Suppress Output, while debugging
- commonly used is this suppression:
30 1 * * * command > /dev/null 2>&1
- re-enable the standard output or standard error message output
- commonly used is this suppression:
Still not working? Yikes!
- Raise the cron debug level
- Debian
- in
/etc/default/cron
- set
EXTRA_OPTS="-L 2"
service cron restart
tail -f /var/log/syslog
to see the scripts executed
- in
- Ubuntu
- in
/etc/rsyslog.d/50-default.conf
- add or comment out line
cron.crit /var/log/cron.log
- reload logger
sudo /etc/init.d/rsyslog reload
- re-run cron
- open
/var/log/cron.log
and look for detailed error output
- in
- Reminder: deactivate log level, when you are done with debugging
- Debian
- Run cron and check log files again
Cronjob Syntax
# Minute Hour Day of Month Month Day of Week User Command