Loading...


Related Products


Date Published: February 6, 2025

Debugging cron

General

There is an extensive page on drupal.org for solving problems with cron: Solving cron problems.

There are a few modules aimed at helping debug cron. The one most commonly used is Cron debug for Drupal 7 only.

If you're having problems with cron and search (or think you might be), see Debugging Cron / Search Indexing.
 

Note
For Acquia Cloud customers, please see the Acquia Cloud page, Using scheduled jobs to support your application, for information on redirecting debug output, and using the cron-wrapper.sh script for debugging.
 


There are a number of ways to debug issues with cron that are explored here :

 

Using Drush

Drush offers a multitude of methods to troubleshoot cron and just about everything else.
In this section, we present two methods for debugging cron.

Modules

For Drupal 7 only, the Drush Debug Tools are a group of extensions for Drush, written by Acquians, with more extensive cron debug capabilities.

Using php-eval

You can debug cron by using the Drush utility using php-eval.

The code below runs drush php-eval using the respective functions :

The code logs each part of cron as cron runs, and then outputs that to the terminal.

There is a version of the drush php-eval code below, for Drupal 7, and for the current Drupal version, respectively.

Code For Drupal 7

drush php-eval 'global $timers; $return = $args = array();foreach (module_implements("cron") as $module) {  $function = $module . "_cron"; print($function ." - ");  timer_start($function); $result = call_user_func_array($function, $args);  if (isset($result) && is_array($result)) { $return = array_merge_recursive($return, $result); }  else if (isset($result)) { $return[] = $result; }  timer_stop  ($function);  print($timers[$function]["time"] ."\r\n");} '

This code will return results like:

ctools_cron - 9.74
field_cron - 83.14
node_cron - 3.91
search_cron - 1.97
system_cron - 1282.06
trigger_cron - 7.53
update_cron - 1036.85

Code For Current Drupal version

drush php-eval 'use Drupal\Component\Utility\Timer; foreach(array_keys(\Drupal::moduleHandler()->getModuleList()) as $mod_name) {if(\Drupal::moduleHandler()->hasImplementations("cron", $mod_name)) {print("** Starting execution of " . $mod_name . " cron (" . $mod_name . "_cron()) ... "); Timer::start("time_".$mod_name); \Drupal::moduleHandler()->invoke("cron",$mod_name); Timer::stop("time_".$mod_name); print("Completed Execution of " . $mod_name . "_cron() - time taken:\t" . Timer::read("time_".$mod_name) . "ms" . PHP_EOL . PHP_EOL);} }'

This code will return results like:

** Starting execution of captcha cron (captcha_cron()) ... Completed Execution of captcha_cron() - time taken:  0.01ms

** Starting execution of dblog cron (dblog_cron()) ... Completed Execution of dblog_cron() - time taken:        0.01ms

** Starting execution of field cron (field_cron()) ... Completed Execution of field_cron() - time taken:        0.01ms

** Starting execution of file cron (file_cron()) ... Completed Execution of file_cron() - time taken:   0.01ms

** Starting execution of honeypot cron (honeypot_cron()) ... Completed Execution of honeypot_cron() - time taken:       0.01ms

** Starting execution of layout_builder cron (layout_builder_cron()) ... Completed Execution of layout_builder_cron() - time taken:     0.01ms

** Starting execution of locale cron (locale_cron()) ... Completed Execution of locale_cron() - time taken:     0.01ms

** Starting execution of node cron (node_cron()) ... Completed Execution of node_cron() - time taken:   0.01ms

** Starting execution of purge_processor_cron cron (purge_processor_cron_cron()) ... Completed Execution of purge_processor_cron_cron() - time taken:0.01ms

** Starting execution of scheduler cron (scheduler_cron()) ... Completed Execution of scheduler_cron() - time taken:    0.01ms

** Starting execution of search_api cron (search_api_cron()) ... Completed Execution of search_api_cron() - time taken: 0.01ms

** Starting execution of simple_sitemap cron (simple_sitemap_cron()) ... Completed Execution of simple_sitemap_cron() - time taken:     0.01ms

** Starting execution of system cron (system_cron()) ... Completed Execution of system_cron() - time taken:     0.01ms

** Starting execution of update cron (update_cron()) ... Completed Execution of update_cron() - time taken:     0.01ms

** Starting execution of password_policy cron (password_policy_cron()) ... Completed Execution of password_policy_cron() - time taken:  0.01ms


Modifying Drupal Core (Drupal 7 only)

Note
It is not encouraged to modify core, especially for a Production environment! This should be used as a last resort.


Alternately, you can use the following code to help you debug cron. The code is a replacement function that goes in includes/module.inc in place of the existing module_invoke_all() function (for Drupal 7). It logs each part of cron as cron runs, and then logs that to the system log.

There is a version for Drupal 7 :

<?php
/**
 * Invokes a hook in all enabled modules that implement it.
 *
 * @param $hook
 *   The name of the hook to invoke.
 * @param ...
 *   Arguments to pass to the hook.
 *
 * @return
 *   An array of return values of the hook implementations. If modules return
 *   arrays from their implementations, those are merged into one array.
 */
function module_invoke_all($hook) {
  $args = func_get_args();
  // Remove $hook from the arguments.
  unset($args[0]);
  $return = array();
  foreach (module_implements($hook) as $module) {
    // BEGIN CRON LOG
    if ($hook == "cron") {
      watchdog('cron', 'Running cron for: %module', array('%module' => $module), WATCHDOG_NOTICE);
    }
    // END CRON LOG
    $function = $module . '_' . $hook;
    if (function_exists($function)) {
      $result = call_user_func_array($function, $args);
      if (isset($result) && is_array($result)) {
        $return = array_merge_recursive($return, $result);
      }
      elseif (isset($result)) {
        $return[] = $result;
      }
    }
  }

  return $return;
}
?>
 
Note
Remember to remove this code when you're not actively debugging a problem, because it will fill your logs with additional logging during each cron run.

Did not find what you were looking for?

If this content did not answer your questions, try searching or contacting our support team for further assistance.

Back to Section navigation