function hook_cron

You are here

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

Perform periodic actions.

Modules that require to schedule some commands to be executed at regular intervals can implement hook_cron(). The engine will then call the hook at the appropriate intervals defined by the administrator. This interface is particularly handy to implement timers or to automate certain tasks. Database maintenance, recalculation of settings or parameters, and automatic mailings are good candidates for cron tasks.

Return value

None.

This hook will only be called if cron.php is run (e.g. by crontab).

Related topics

13 functions implement hook_cron()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

aggregator_cron in modules/aggregator/aggregator.module
Implementation of hook_cron().
dblog_cron in modules/dblog/dblog.module
Implementation of hook_cron().
filter_cron in modules/filter/filter.module
Implementation of hook_cron().
node_cron in modules/node/node.module
Implementation of hook_cron().
openid_cron in modules/openid/openid.module
Remove expired nonces from the database.

... See full list

1 invocation of hook_cron()
drupal_cron_run in includes/common.inc
Executes a cron run when called

File

developer/hooks/core.php, line 293
These are the hooks that are invoked by the Drupal core.

Code

function hook_cron() {
  $result = db_query('SELECT * FROM {site} WHERE checked = 0 OR checked
    + refresh < %d', time());

  while ($site = db_fetch_array($result)) {
    cloud_update($site);
  }
}

Comments

In drupal 6.x (I've tested 6.15-6.19) hook_cron won't run if the name of the file in which the hook is called is cron.php (in your module folder). Actually, no hooks, functions, or stray code will be called in a file called cron.php in your module folder. You will not receive any php errors running cron, and none of the code will execute. "Include" commands for the file will simply be ignored and no errors will be given. I don't know if this applies to subfolders as well, haven't tested that. I don't know if this is already documented but since this is the only real documentation on hook_cron out there that I could find, I thought this might be helpful to others banging their heads against the wall.

Armsch6,

Remember Drupal loads <name_of_your_module>.module files and execute the <name_of_your_mobule>_cron() function (generically named hook_cron in documentation). If you want your file cron.php to be included and hook_cron executed you must explicitly tell Drupal to load it. For example create a mymodule.module file including these code lines:

<?php
require_once('cron.php');
?>

In your cron.php you will have to define a function like this :

<?php
function mymodule_cron() {
 
// Your code here
}
?>

I am afraid there is a confusion with the cron.php file (belonging to the Drupal core) which must be called at regular intervals in order to execute hook_cron() functions implemented in each enabled module. This can be done programming a crontab or calling this file with an external tool, like webcron. A typical crontab may look like this (replace www.example.com with your own website domain name) :

# m h  dom mon dow   command
*/10 * * * * wget -O - -q -t 1 http://www.example.com/cron.php

<?php
require_once('cron.php');
?>

This will not load the file called cron.php in the module's directory. That is what I meant. I wouldn't go as far as to call this a bug, as putting a file called "cron.php" in a module's directory is probably not good practice anyway, but it's certainly an unintended phenomenon...

Sigh. This is because PHP's working directory is Drupal's root. So this command includes Drupal's cron.php.

Here's the best practice way to put your cron code outside of your .module file:

Stick a stub hook_cron() function in your .module file that uses module_load_include() to load your MODULE.cron.inc file, and calls the function in that file that does the actual work (usually you'll call it _MODULE_cron()).

Sorry, so whats the conclusion here ?
I have my module installed and I have also implemented hook_cron() in my .module file.
Now when, I open a browser and say, http://www.example.com/cron.php , i see that my hook_cron() is not getting hit ?
How do I proceed ?

Thanks

I'm just starting this today, but isn't "hook" replaced with your module name.. i.e. "mymodule_cron()"?

That is correct. Just replace the 'hook' part of the function with the name of your module. e.g: function awesomemodule_cron() or function awesomemodule_menu()...