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."

5
  • 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
    Commented Dec 12, 2011 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
    Commented Mar 11, 2013 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
    Commented Mar 12, 2013 at 16:24
  • Just run: drush sqlq "DELETE FROM semaphore WHERE name = 'cron';"
    – kenorb
    Commented Feb 18, 2014 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.
    – avpaderno
    Commented Jul 31, 2014 at 21:05

11 Answers 11

12

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
6
  • 3
    Or delete it manually from the variables table if you have db access rather than drush.
    – Malks
    Commented Dec 13, 2011 at 1:40
  • 8
    Don't forget these values are cached in the table cache_bootstrap.
    – tostinni
    Commented Dec 13, 2011 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 Commented Mar 11, 2013 at 15:40
  • 2
    When i run the drush command above I get "cron_semaphore not found." Any ideas? Commented Apr 23, 2013 at 20:18
  • 1
    @NigelWaters The cron semaphore became a lock, in Drupal 7, but drupal_cron_cleanup() seems not to be updated.
    – avpaderno
    Commented Jul 31, 2014 at 21:03
19

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.

4
  • 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
    Commented Sep 11, 2013 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 ? Commented Sep 12, 2013 at 9:17
  • 1
    This is the correct answer for Drupal 7. +1 for the detailed explanation.
    – avpaderno
    Commented Jul 31, 2014 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/… Commented Mar 8, 2018 at 21:10
4

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 
1
  • 3
    "Less extreme" variant for Drupal 7: drush sql-query "DELETE FROM semaphore WHERE name='cron'"
    – tanius
    Commented Nov 8, 2015 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.

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

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
0

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

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.

0

You could try the following:

1. Release the cron lock

In Drupal 8+, the cron lock is in the "semaphore" table with the key name "cron". This can be removed using this Drush command:

drush sqlq "DELETE FROM semaphore WHERE name = 'cron';"

Or if you don't have Drush, and you can execute PHP in the Drupal site (e.g. using the Devel PHP module), you can run one of the following:

\Drupal::database()->query("DELETE FROM semaphore WHERE name = 'cron';");
\Drupal::lock()->release('cron');

2. Find the offending error in the error log

The error log is at "Reports > Recent log messages" (/content/admin/reports/dblog).

If the error log is filled with message saying "Attempting to re-run cron while it is already running.", then you might need to disabled the automatic cron temporarily while you find the issue:

3. Disable cron

Go to the cron settings (/content/admin/config/system/cron) and change "Run cron every" to "never".

4. Find and uninstall the offending module/s

There's a good chance that the issue is caused by some process that a module is trying to run in a cron hook. The module might not be problematic in itself; it might be an incorrect setting that's causing the process to fail.

5. Re-enable cron

On most sites, cron should be set to run every 1 or 3 hours. By default, it will run in "poor man's cron" module, meaning it will run only when people visit the site. However it can be run more regularly by triggering it with a "cron job" on the server (requires advanced setup).

0

A quick universal solution for Drupal 7, 8, 9, 10, 11 to unlock cron, stuck in the "already running" status is to use this Drush command:

drush sql:query "DELETE FROM semaphore WHERE name='cron'"
-1

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.

3
  • Welcome to Drupal Answers! The question is about Drupal 7. Deleting rows from that database table would not have much effect.
    – avpaderno
    Commented Jul 31, 2014 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
    Commented Sep 9, 2014 at 9:16
  • just updated to remove the run cron from command line part, just found out it can cause problems
    – munkiepus
    Commented Jan 13, 2015 at 16:41
-2

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 and acknowledge you have read our privacy policy.

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