Run drush cron from crontab

Posted by: 
Dominique De Cooman

As discussed in previous blog post, drush cron can be run from crontab too. While seeming pretty straightforward, trying it out caused some troubles.

The thing you would do to make drush run from crontab is:

$ crontab -e
* * * * * drush -r /path/to/drupal

Also tried to give full path to drush install

$ crontab -e
* * * * * /usr/local/bin/drush -r /path/to/drupal

This didnt work for me. To see the output of your command:

$ tail -f /var/mail/root

This gave me:

Command core-cron needs a higher bootstrap level to run - you will [error]
need invoke drush from a more functional Drupal environment to run
this command.
The drush command 'cron' could not be executed. [error]
Drush was not able to start (bootstrap) the Drupal database. [error]
Hint: This error often occurs when Drush is trying to bootstrap a
site that has not been installed or does not have a configured
database.

Why? It seems the environment variables arent the same when running the command in terminal as in from crontab.

Try this:

$ crontab -e
* * * * * env > /tmp/cronenv.log

This will output the env vars used when crontab executes the command.
Then in terminal print the normal env vars:
$ env > /tmp/bashenv.log

Now diff the two:
$ diff /tmp/bashenv.log /tmp/cronenv.log

In my case copying PATH to the head of crontab did the trick:

$ crontab -e
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
0 * * * * /usr/bin/drush -r /path/to/drupal cron

Add new comment