Drupal Cron Troubleshooting

July 4, 2009 · 5 comments

in Drupal

If you’re having problems with Drupal cron — like cron runs are timing out or running out of memory — here’s a quick hack to figure out which module is causing the problems.

Edit the includes/module.inc file, and find the function module_invoke_all.  Insert the line indicated below to have a notice added to the logs (admin/logs/watchdog) for each module cron is running. You can then figure out the last module running when the problems occur.

foreach (module_implements($hook) as $module) {
  $function = $module .'_'. $hook;
  if ($hook == 'cron') watchdog('cron', "hit $module cron");   // add this line
  ...
}

Thanks to Peter Lindstrom for the tip.

UPDATE:

The Drupal cron job crashes but I keep getting this error in the watchdog logs: “Attempting to re-run cron while it is already running.”

Then it gets killed after an hour with this message: “Cron has been running for more than an hour and is most likely stuck.”

To force Drupal to allow cron jobs to run again, you have to:

  1. Delete the “cron_semaphore” variable from the database.  (Hat tip to cyberswat on drupal.org.)
  2. Clear the Drupal cache to reload the variables.  (Install the Devel module and go to example.com/devel/cache/clear to clear the cache quickly.)

There’s a Drupal variable that is created when cron starts, and is supposed to get deleted when cron ends. If the cron job crashes, the variable is left behind and no new cron jobs can run until the variable gets policed up in an hour.

To clear the cron lock, delete the “cron_semaphore” variable in your database.

mysql> select * from variable where name = 'cron_semaphore';
+----------------+---------------+
| name           | value         |
+----------------+---------------+
| cron_semaphore | i:1246773950; |
+----------------+---------------+
1 row in set (0.00 sec)

mysql> delete from variable where name = 'cron_semaphore';
Query OK, 1 row affected (0.00 sec)
  • Gawrion

    Hey – that was my third attempt to solve my problem with crone. Googled “drupal cron mysql” i i easily find your very pragmatic way to solve cron-problem!!

    Your article is very helpful.. Most of us dont know architecture of drupal soft. Your debugging hint solves all problems..

    ThankS!! :)

  • Gawrion

    One more thing.. I tohought that maybe if you could check out our drupal site at http://niepoprawni.pl, you could find whats is possible to optimize etc. Obviously that would be some kind of deal ;) If you want to talk – plzz contact us at niepoprawni.pl/contact form.

  • Ankleface Wroughtlandmire

    Thanks very much for this debugging tip. I was pulling out my hair. Finally found the problem. Any reason I shouldn't just leave this “Hit” code in there all the time?

  • http://avocadoshake.net greggo

    Two reasons to not leave the code in all the time. I don't think it'll break anything, but there are no real benefits either:

    1. It's adding extra db queries and cruft to the watchdog logs that you don't need, except to troubleshoot this particular problem.  

    2. When you update Drupal core, the hack will get overwritten.

  • Ankleface Wroughtlandmire

    Ok, thanks again for the help! I really appreciate it.

Previous post:

Next post: