0

I have a module that needs to behave differently when its hooks are invoked during a cron run. (For example, my module should redirect normal page requests, but avoid redirecting during a cron run, so that the cron run is not interrupted.)

How can I programmatically determine whether the current request is part of a cron run?

0

Implement hook_cron_queue_info() to set a global flag indicating that the current request is part of a cron run.

/**
 * Implements hook_cron_queue_info().
 *
 * hook_cron_queue_info rather than hook_cron, to ensure it executes before
 * any hook_cron implementations.
 */
function mymodule_cron_queue_info() {
  global $mymodule_in_cron;
  $mymodule_in_cron = TRUE;
}

Then, elsewhere in your module, you can check this flag:

global $mymodule_in_cron;
if (!empty($mymodule_in_cron)) {
  return;
}

This works both when invoking cron via the web server and via drush 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.