Running Drupal cron tasks from drush

Drupal cron tasks are often set up to be run via a wget call to cron.php; this same task can also be accomplished via the `drush cron` command, which circumvents the need to provide a webserver interface to cron.


If you just want to get started quickly, here is a crontab entry that will run cron once every hour at ten minutes after the hour:

10 * * * * /usr/bin/env PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin COLUMNS=72 /usr/local/drush/drush --root=/path/to/your/drupalroot --quiet cron

You should set up crontab to run your cron tasks as the same user that runs the web server; for example, if you run your webserver as the user www-data:

sudo -u www-data crontab -e

You might need to edit the crontab entry shown above slightly for your paricular setup; for example, if you have installed drush to some directory other than /usr/local/drush, then you will need to adjust the path to drush appropriately. We'll break down the meaning of each section of the crontab entry in the documentation that continues below.

Setting the schedule

See `man 5 crontab` for information on how to format the information in a crontab entry. In the example above, the schedule for the crontab is set by the string "10 * * * *". These fields are the minute, hour, day of month, month and day of week; "*" means essentially "all values", so "10 * * * *" will run any time the minute == 10 (once every hour).

Setting the PATH

We use /usr/bin/env to run drush so that we can set up some necessary environment variables that drush needs to execute. By default, cron will run each command with an empty PATH, which would not work well with drush. To find out what your PATH needs to be, just type:

echo $PATH

Take the value that is output and place it into your crontab entry in the place of the one shown above. You can remove any entry that is known to not be of interest to drush (e.g. /usr/games), or is only useful in a graphic environment (e.g. /usr/X11/bin).


When running drush in a terminal, the number of columns will be automatically deteremined by drush by way of the tput command, which queries the active terminal to determine what the width of the screen is. When running drush from cron, there will not be any terminal set, and the call to tput will produce an error message. Spurrious error messages are undesirable, as cron is often configured to send email whenever any output is produced, so it is important to make an effort to insure that successful runs of cron complete with no output.

In some cases, drush is smart enough to recognize that there is no terminal -- if the terminal value is empty or "dumb", for example. However, there are some "non-terminal" values that drush does not recognize, such as "unknown." If you manually set COLUMNS, then drush will repect your setting and will not attempt to call tput.

Using --quiet

By default, drush will print a success message when the run of cron is completed. The --quiet flag will suppress these and other progress messages, again avoiding an unnecessary email message.

Specifying the Drupal site to run

There are many ways to tell drush which Drupal site to select for the active command, and any may be used here. The example uses the --root and --uri flags, but you could also use an alias record if you defined it in a global location, such as /etc/drush/aliases.drushrc.php.