function cron_example_cron

Same name in other branches
  1. 3.x modules/cron_example/cron_example.module \cron_example_cron()
  2. 4.0.x modules/cron_example/cron_example.module \cron_example_cron()

Implements hook_cron().

hook_cron() is the traditional (pre-Drupal 7) hook for doing "background" processing. It gets called every time the Drupal cron runs and must decide what it will do.

In this example, it does a watchdog() call after the time named in the variable 'cron_example_next_execution' has arrived, and then it resets that variable to a time in the future.

Related topics

File

cron_example/cron_example.module, line 170

Code

function cron_example_cron() {
    // Default to an hourly interval. Of course, cron has to be running at least
    // hourly for this to work.
    $interval = variable_get('cron_example_interval', 60 * 60);
    // We usually don't want to act every time cron runs (which could be every
    // minute) so keep a time for the next run in a variable.
    if (time() >= variable_get('cron_example_next_execution', 0)) {
        // This is a silly example of a cron job.
        // It just makes it obvious that the job has run without
        // making any changes to your database.
        watchdog('cron_example', 'cron_example ran');
        if (!empty($GLOBALS['cron_example_show_status_message'])) {
            drupal_set_message(t('cron_example executed at %time', array(
                '%time' => date_iso8601(time(0)),
            )));
        }
        variable_set('cron_example_next_execution', time() + $interval);
    }
}