3

Is there any way programatically to determine when the cron is running (and if possible, what tasks are being run)? I've explored some options online that are bound to drupal_cron_run(), but no examples on how to listen for this method being used, as it is intended to start the cron.

8

In Drupal 7:

To determine if the current page request is a request for cron: in other words, "is my code being called during cron"?

If you're running cron through cron.php, this is probably sufficient:

return strpos($_SERVER['REQUEST_URI'], 'cron.php') !== 0;

A more reliable way to determine a cron would be through introspection (e.g. debug_backtrace()), but that would entail a much greater degree of overhead.

To determine if any cron request is still running, across all Drupal threads and pageloads: in other words, "is any process running cron anywhere?" * Note that this method will fail if cron is stuck.

Drupal will programmatically set a variable called cron_semaphore while the cron is running. This variable is deleted once cron has completed. You can check for the presence of this variable to determine whether cron is currently active or not:

if (variable_get('cron_semaphore', FALSE)) {
  // cron is running
}

You can see the specific use of cron_semaphore in the documentation of drupal_cron_run().

The 'tasks being ran' will be implementations of hook_cron, which you can find using:

foreach (module_implements('cron') as $module) {
  $function = $module . '_cron';

  drupal_set_message($function . '() is being ran');
}
1

There is a variable you can check which is called cron_last. It will be a timestamp of the last completed cron run.

So, something like variable_get('cron_last')

  • This doesn't answer the original question. What difference does it make when it was started? Is it still going? – Magmatic Jul 20 '18 at 20:32

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.