12

What are the steps to troubleshoot and resolve this issue as per title?

When I try to run manually, I get a page immediately, and the error message "Attempting to re-run cron while it is already running."

  • I couldn't remember off the top of my head what in the db cron looks at, but a quick google of "attempting to re-run cron while it is already running" pointed me to digitaledgesw.com/node/21, it might apply to D7 – Jimajamma Dec 12 '11 at 14:40
  • That is very strange. That message is found exactly once in the code for core, right after a check to that variable (via lock_acquire in drupal_cron_run). If you post more info/symptoms that point that it is indeed different, I will re-open it. – mpdonadio Mar 11 '13 at 18:43
  • Suggestion: can you try to install a fresh Drupal and set cron to run every 3 minutes and see if you have the same effect!!! – Aboodred1 Mar 12 '13 at 16:24
  • Just run: drush sqlq "DELETE FROM semaphore WHERE name = 'cron';" – kenorb Feb 18 '14 at 15:01
  • 4
    TO FUTURE READERS: The currently accepted answer is not correct for Drupal 7, the Drupal version for which the question is. It is valid for Drupal 6, but Drupal 7 code changed; it just happens that Drupal 7 has remains from Drupal 6 that have not been cleaned up. – kiamlaluno Jul 31 '14 at 21:05
13

The cron semaphore is probably locked. You could try calling drupal_cron_cleanup() from anywhere in your code (that doesn't happen to be invoked by cron) and that should unlock your cron semaphore variable.

If you have drush configured in Drupal 6 you could also try:

$ drush vdel -y cron_semaphore
  • 3
    Or delete it manually from the variables table if you have db access rather than drush. – Malks Dec 13 '11 at 1:40
  • 8
    Don't forget these values are cached in the table cache_bootstrap. – tostinni Dec 13 '11 at 18:23
  • 1
    please what is the exact name of the table because i didn't find any cron_semaphore in the variables table – mohammed amine bourkadi Mar 11 '13 at 15:40
  • 2
    When i run the drush command above I get "cron_semaphore not found." Any ideas? – Nigel Waters Apr 23 '13 at 20:18
  • 1
    @NigelWaters The cron semaphore became a lock, in Drupal 7, but drupal_cron_cleanup() seems not to be updated. – kiamlaluno Jul 31 '14 at 21:03
20

Summary

The error message you are seeing would happen if the following two conditions are met:

  • You cron jobs do not finish (error) or take more than 4 minutes to finish ;
  • You invoke cron more often than every 4 minutes (According to your crontab setting, not the setting in Drupal)

This error message is therefore a symptom of a cron tasks that is either failing or taking too long to run. (Note: I realise you have already found the culprit, but I wanted to add an answer for people who find this page via searches, as I did)

Background

The first thing to understand is how Drupal cron tasks are run. The Drupal cron is invoked at regular intervals - either through a cron job on your server, or after every page load if you use the poor man's cron which is Drupal's default.

The cron tasks are not necessarily run every time cron is invoked however - there is a setting in Drupal (default is 3 hours) which says how often cron tasks should be run. But this delay of 3 hours only applies if the cron tasks have finished successfully.

In Drupal 7, cron uses Drupal's locking mechanisms, which provides a cooperative, advisory lock system. One of the features of this lock system is that locks expire after a certain time. In the case of cron, it expires after 4 minutes - so if your cron is invoked every 3 minutes and the previous cron job hadn't finished by that time (either it crashed or was very slow), you would indeed get this error message.

The fact you set the cron to be every 12 hours does not make a difference - because the Drupal cron task is failing/taking too long, Drupal assumes it hasn't been run so tries to run it again as soon as cron is invoked. The twelve hour delay only applies to successful cron runs.

The cron semaphore variable does not exist any more in Drupal 7 - this was for an older version of Drupal. In Drupal 7 there is no reliable way to manually release a lock, because the locking backend might change - however if you are using the core locking mechanism then you can release cron locks by editing the database:

DELETE FROM semaphore WHERE name = 'cron';

But by doing this you would only be fixing the symptoms - the problem that needs addressing is why is cron failing / taking so long to run.

  • I am getting this message, but when I run cron_debug all of the cron jobs end successfully and run quickly (except for core updates which takes ~5 seconds). If the source of this problem is slow or failing cron routines, why wouldn't cron_debug highlight them? – doub1ejack Sep 11 '13 at 18:01
  • 1
    I don't know how the cron_debug module works, so I can't really comment on this. What is the value of the variable 'cron_safe_threshold' ? What data is the 'semaphore' table ? – Alice Heaton Sep 12 '13 at 9:17
  • 1
    This is the correct answer for Drupal 7. +1 for the detailed explanation. – kiamlaluno Jul 31 '14 at 21:07
  • BTW, if cron is failing for you, use the Cron Debug modules to see what module it hangs up on - 9 times out of 10 it's the Search module. Try turning the search module off then run cron manually and see what happens. To find the nodes it's trying to index: drupal.stackexchange.com/questions/22825/… – Dan Mantyla Mar 8 '18 at 21:10
5

The cron_semaphore variable did exist in Drupal 6, but you're using Drupal 7, so the semaphore locks moved into separate table called semaphore.

So solution to unlock the cron semaphore would be:

Drupal 7

drush sqlq "TRUNCATE semaphore"

Drupal 6

drush -y vdel cron_semaphore 
  • 3
    "Less extreme" variant for Drupal 7: drush sql-query "DELETE FROM semaphore WHERE name='cron'" – tanius Nov 8 '15 at 23:56
2

You can debug the cron by using Cron Debug module.

Cron Debug will help you find cron processes which

  • fail due to programming or runtime errors
  • time out (PHP, server, database)
  • are very slow

Cron Debug will also allow you to test run specific cron functions while not running others. This can be nice for developing cron functions where you don't want to run a full cron.php with all maintenance, alerts and other tasks every time you test your own function.

  • 1
    This module was a life saver for me! – Dan Mantyla Mar 8 '18 at 21:11
  • yeah, this module is good, it helps to debug the cron job. – Nishant Pandya Mar 12 '18 at 7:28
2

This is how I solved the cron issue:

  1. Via PhpMyAdmin, I looked in the semaphore table and deleted row where name = cron
  2. I ran "drush cron" via command line
  3. I seen an error message that gave me a clue of the issue; this may be different for you, but for me a recently uninstalled module that was sharing a PHP library with an installed module was causing an error
1

For Drupal 8 : drush sqlq "DELETE FROM semaphore WHERE name = 'cron';"

0

I've come up against this a few times. In Drupal 6 If you don't use drush you can fix it like this:

1 Remove the cron_% variables from the variables table.

SELECT * FROM variable WHERE name like "cron%";

DELETE FROM variable WHERE name like "cron%";

2 clear Drupal cache at: /admin/settings/performance

by pressing the 'clear cached data' button at the bottom of the page.

3 run cron from admin panel /admin/reports/status/run-cron don't run it from comand line as this can cause problems.

4 Check that the next automatic cron run completes as normal.

  • Welcome to Drupal Answers! The question is about Drupal 7. Deleting rows from that database table would not have much effect. – kiamlaluno Jul 31 '14 at 20:55
  • Ah i only added the answer as it hadn't said specifically that it was for Drupal 7 in the OP. That works well for drupal 6, and is more complete than the usual 'delete the semaphore' answers. i figured it would be helpful if anyone ended up here from d6. Was that wrong? I seem to have been marked down for it. – munkiepus Sep 9 '14 at 9:16
  • just updated to remove the run cron from command line part, just found out it can cause problems – munkiepus Jan 13 '15 at 16:41
0

On Drupal 7, as even another option,

Via drush cron I was getting,

WD cron: Attempting to re-run cron while it is already running.       [warning]
Cron run failed.                                                      [error]

This was on development environment and happened because a previous cron was running and got interrupted.

On includes/common.inc, at line 5413, or around, you'll find this block, https://cgit.drupalcode.org/drupal/tree/includes/common.inc?h=7.x#n5413

  // Try to acquire cron lock.
  if (!lock_acquire('cron', 240.0)) {
    // Cron is still running normally.
    watchdog('cron', 'Attempting to re-run cron while it is already running.', array(), WATCHDOG_WARNING);
  }

Modify the if with an && FALSE,

  // Try to acquire cron lock.
  if (!lock_acquire('cron', 240.0) && FALSE) {
    // Cron is still running normally.
    watchdog('cron', 'Attempting to re-run cron while it is already running.', array(), WATCHDOG_WARNING);
  }

And rerun cron. That will allow it to run.

Please don't deploy this and revert it back after it finishes. It also helps with debugging.

-1

You can debug by using xdebug , run cron from admin interface Admin > Configuration > system > cron.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.