Restored guide
Getting more control over your cron hooks and executing cron hooks on the shell as separate crontabs
Drupal's cron runs every hook_cron() in one request. A slow job then blocks the others, and a PHP timeout looks like a random failure. Split the work by giving heavy jobs their own schedule.
What you are solving
Search indexing, mail queues, and ImageCache maintenance do not share a deadline. Elysia Cron, on Drupal 6, is the usual way to give each hook its own rule without abandoning hook_cron(). You can also invoke one callback from a Drush script when you want the schedule to live in crontab rather than in the database.
See who is registered
On a development copy, list cron hooks before you change the schedule. Elysia Cron exposes a settings page for this. From Drush, a short script can call the same registry the site uses and print module names. Run it as the site user so bootstrap finds the right settings.php.
cd /var/www/drupal
drush php-eval "print_r(module_implements('cron'));"
Give a heavy hook its own rule
Leave lightweight hooks on the default run. Move the expensive one to a channel you can watch. With Elysia Cron, set a rule such as */15 * * * * only for that module, and keep the default run hourly. From the shell, the equivalent is a script that bootstraps Drupal and invokes a single implementation.
// scripts/cron-search.php — run with: drush scr ../scripts/cron-search.php
if (!function_exists('drush_bootstrap')) {
return;
}
search_cron();
Replace search_cron() with the implementation you timed. Do not include the script from the web. Keep it outside the docroot or deny it in Apache.
Crontab
0 * * * * cd /var/www/drupal && drush cron >> /var/log/drupal-cron.log 2>&1
*/15 * * * * cd /var/www/drupal && drush scr /opt/drupal-scripts/cron-search.php >> /var/log/drupal-cron.log 2>&1
Check before you deploy
- Run each command once in the foreground and read the log.
- Confirm two overlapping runs cannot corrupt the same queue. A lock around the heavy job is enough.
- Watch memory. A hook that was safe inside a short request can grow when it is allowed to finish.
Restored guide for the 5 February 2011 note on cron hooks and separate crontabs.