How to debug not running Cron jobs

Below is the steps to debug the cron job execution:

  1. Is the Cron daemon running?
  • Run ps ax | grep cron and look for cron.
  • Debian: service cron start or service cron restart
  1. Is cron working?

  • * * * * * /bin/echo “cron works” >> /file
  • Syntax correct? See below.
  1. 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
  1. 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
  1. 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
  1. 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
  1. 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

Still not working? Yikes!

  1. 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
  • 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
  • Reminder: deactivate log level, when you are done with debugging
  1. Run cron and check log files again

Cronjob Syntax

# Minute  Hour  Day of Month      Month         Day of Week    User Command

# (0-59) (0-23)   (1-31)    (1-12 or Jan-Dec) (0-6 or Sun-Sat)

0       2       *             *                *          root /usr/bin/find

Crontab Commands

  1. crontab -l
  • Lists all the user’s cron tasks.
  1. crontab -e, for a specific user: crontab -e -u agentsmith
  • Starts edit session of your crontab file.
  • When you exit the editor, the modified crontab is installed automatically.
  1. crontab -r
  • Removes your crontab entry from the cron spooler, but not from cronta
Advertisements


Categories: Linux

Tags: ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: