cron.html

  1. 6.x docs/cron.html
  2. 5.x docs/cron.html

File

docs/cron.html
View source
<h1>Running Drupal cron tasks from drush</h1>
<p>
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.

<h2>Quickstart</h2>
<p>
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:
<p>
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 --uri=your.drupalsite.org --quiet cron
<p>
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:
<p>
sudo -u www-data crontab -e
<p>
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.

<h2>Setting the schedule</h2>
<p>
See <b>`man 5 crontab`</b> 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).

<h2>Setting the PATH</h2>
<p>
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:
<p>
echo $PATH
<p>
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).

<h2>Setting COLUMNS</h2>
<p>
When running drush in a terminal, the number of columns will be
automatically determined 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.
<p>
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.

<h2>Using --quiet</h2>
<p>
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.

<h2>Specifying the Drupal site to run</h2>
<p>
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.