Community Documentation

hook_cron

5 core.php hook_cron()
6 core.php hook_cron()
7 system.api.php hook_cron()
8 system.api.php hook_cron()

Perform periodic actions.

Modules that require some commands to be executed periodically can implement hook_cron(). The engine will then call the hook whenever a cron run happens, as defined by the administrator. Typical tasks managed by hook_cron() are database maintenance, backups, recalculation of settings or parameters, automated mailing, and retrieving remote data.

Short-running or non-resource-intensive tasks can be executed directly in the hook_cron() implementation.

Long-running tasks and tasks that could time out, such as retrieving remote data, sending email, and intensive file tasks, should use the queue API instead of executing the tasks directly. To do this, first define one or more queues via hook_cron_queue_info(). Then, add items that need to be processed to the defined queues.

Related topics

▾ 21 functions implement hook_cron()

AggregatorCronTestCase::testCron in modules/aggregator/aggregator.test
Add feeds update them on cron.
aggregator_cron in modules/aggregator/aggregator.module
Implements hook_cron().
common_test_cron in modules/simpletest/tests/common_test.module
Implements hook_cron().
common_test_cron_helper_cron in modules/simpletest/tests/common_test_cron_helper.module
Implements hook_cron().
CronRunTestCase::testAutomaticCron in modules/system/system.test
Ensure that the automatic cron run feature is working.
DBLogTestCase::verifyCron in modules/dblog/dblog.test
Verify cron applies the dblog row limit.
dblog_cron in modules/dblog/dblog.module
Implements hook_cron().
field_cron in modules/field/field.module
Implements hook_cron().
node_cron in modules/node/node.module
Implements hook_cron().
openid_cron in modules/openid/openid.module
Remove expired nonces from the database.
poll_cron in modules/poll/poll.module
Implements hook_cron().
search_cron in modules/search/search.module
Implements hook_cron().
statistics_cron in modules/statistics/statistics.module
Implements hook_cron().
system_cron in modules/system/system.module
Implements hook_cron().
system_run_automated_cron in modules/system/system.module
Run the automated cron if enabled.
system_run_cron in modules/system/system.admin.inc
Menu callback: run cron manually.
tracker_cron in modules/tracker/tracker.module
Implements hook_cron().
TriggerCronTestCase::testActionsCron in modules/trigger/trigger.test
Tests assigning multiple actions to the cron trigger.
trigger_cron in modules/trigger/trigger.module
Implements hook_cron().
UpdateCoreTestCase::testModulePageRunCron in modules/update/update.test
Check that running cron updates the list of available updates.
update_cron in modules/update/update.module
Implements hook_cron().

File

modules/system/system.api.php, line 536
Hooks provided by Drupal core and the System module.

Code

<?php
function hook_cron() {
  // Short-running operation example, not using a queue:
  // Delete all expired records since the last cron run.
  $expires = variable_get('mymodule_cron_last_run', REQUEST_TIME);
  db_delete('mymodule_table')
    ->condition('expires', $expires, '>=')
    ->execute();
  variable_set('mymodule_cron_last_run', REQUEST_TIME);

  // Long-running operation example, leveraging a queue:
  // Fetch feeds from other sites.
  $result = db_query('SELECT * FROM {aggregator_feed} WHERE checked + refresh < :time AND refresh <> :never', array(
    ':time' => REQUEST_TIME, 
    ':never' => AGGREGATOR_CLEAR_NEVER,
  ));
  $queue = DrupalQueue::get('aggregator_feeds');
  foreach ($result as $feed) {
    $queue->createItem($feed);
  }
}
?>
Login or register to post comments