---
title: "Debugging cron"
date: "2025-02-06T01:15:16+00:00"
summary:
image:
type: "article"
url: "/acquia-cloud-platform/help/93006-debugging-cron"
id: "a70ad0aa-a17b-4cfd-b9a4-857f0ab92d73"
---

Table of contents will be added

General
-------

There is an extensive page on drupal.org for solving problems with cron: [Solving cron problems](https://www.drupal.org/node/553430).

There are a few modules aimed at helping debug cron. The one most commonly used is [Cron debug](https://www.drupal.org/project/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](https://support.acquia.com/hc/en-us/articles/360007586193-Debugging-cron-search-indexing).  
 

**Note**  
For Acquia Cloud customers, please see the Acquia Cloud page, [Using scheduled jobs to support your application](/node/56294), 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)
    *   [Modules](#modules) (Drupal 7 only)
    *   [Drush php-eval](#php-eval)
*   [Modify Drupal Core](#modifyingdrupalcore)

 

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](https://www.drupal.org/project/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](http://drupal.org/project/drush) utility using [`php-eval`](https://www.drush.org/12.x/commands/php_eval/).

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

*   **For Drupal 7** - the [module\_implements()](https://api.drupal.org/api/drupal/includes%21module.inc/function/module_implements/7.x) function
*   **For Current Drupal** - the following functions:
    *   [ModuleHandler::getModuleList](https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Extension%21ModuleHandler.php/function/ModuleHandler%3A%3AgetModuleList/10)
    *   [ModuleHandler::hasImplementations](https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Extension%21ModuleHandler.php/function/ModuleHandler%3A%3AhasImplementations/10)
    *   [ModuleHandler::invoke](https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Extension%21ModuleHandler.php/function/ModuleHandler%3A%3Ainvoke/10)

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()](https://api.drupal.org/api/drupal/includes%21module.inc/function/module_invoke_all/7.x) 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.