1 cron_example.module cron_example_cron()

Implements hook_cron().

hook_cron() is the traditional hook for doing "background" processing. It gets called every time the Backdrop 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


modules/examples/cron_example/cron_example.module, line 182
Demonstrates use of the Cron API in Backdrop - hook_cron()


function cron_example_cron() {
  // Default to an hourly interval. Of course, cron has to be running at least
  // hourly for this to work.
  $interval = config_get('cron_example.settings', 'cron_example_interval');
  // 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() >= state_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'])) {
      backdrop_set_message(t('cron_example executed at %time', array('%time' => cron_example_date_iso8601(time(0)))));
    state_set('cron_example_next_execution', time() + $interval);