Search This Blog

Thursday, December 23, 2010

Implementing Cron Jobs in Drupal

Every time you run crontab -e, you may need to be logged in as the administrator of your web server.

Cron can be setup to run a command more or less than once an hour. We need to add the following line to our crontab file to run cron.php at the start of every hour.

0 * * * * /usr/bin/wget -O - -q -t 1 http://www.yoursite.com/cron.php

The 5 characters separated by spaces controls when cron will run the command on the right. Lets break it down with some ascii art:

* * * * * command
| | | | |
| | | | |_day of week (0-6) where Sunday is 0
| | | |
| | | |_month (1-12)
| | |
| | |_day of month (1-31)
| |
| |_hour (0-23)
|
|_minute (0-59)

So say you wanted to run cron.php once every day. The following line would run your drupal cron job every night at midnight:

0 0 * * * /usr/bin/wget -O - -q -t 1 http://www.yoursite.com/cron.php

The next line would run the drupal cron job every day at 2:30pm, or 14:30 military time:

30 14 * * * /usr/bin/wget -O - -q -t 1 http://www.yoursite.com/cron.php

Lets run cron every Sunday and Wednesday at 3am:

0 3 * * 0,3 /usr/bin/wget -O - -q -t 1 http://www.yoursite.com/cron.php

This allows for lots of flexibility in when you'd like to run your drupal cron job.