How to run drupal cron from cli

Posted by: 
Dominique De Cooman

Running cron from cli in drupal will result in.

PHP Warning:  include_once(): Failed opening './includes/bootstrap.inc'

Running cron from cli is useful when your behind external authentication and crontab is not available over http. Because normaly you would put something like:

0 * * * * wget --spider <a href="http://yoursite/cron.php
">http://yoursite/cron.php [/geshifilter-codes]

So here is what you should put in the crontab when you want it to call from cli. (Edit using "crontab -e" as command)

* * * * * php /home/ddcooman/workspace/extranet-client/src/drupal/cron.php

As said this gives the error. So here is the solution:
Create a new cron file "cronlocal.php" containing:


<?php
// $Id: cronlocal.php Exp $

/**
 * @file
 * Handles incoming requests to fire off regularly-scheduled tasks (cron jobs).
 */

$path='/home/ddcooman/workspace/extranet-client/src/drupal';
chdir($path);

include_once 
'./includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
drupal_cron_run();
?>

The chdir will change the directory and bootstrap drupal. After drupal is bootstrapped the drupal cron is executed.

Tip: You can also use this trick to bootstrap drupal for running import scripts. The whole drupal api is available when drupal is bootstrapped.

Comments

How to run drupal cron from cli

drush cron?

How to run drupal cron from cli

Drush cron would be a possibilty. But in our case we do not have control over production envirronment and drush will not be installed there. This means we ll still end up with the sollution proposed.
But you are correct that when drush is available you should use it.

EDIT: And here is how you do it http://dominiquedecooman.com/blog/run-drush-cron-crontab

Add new comment