\hook_cron
function
Perform periodic actions.

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.

Comments

Alan D.’s picture

Unlike many Drupal hooks, DrupalQueueInterface::createItem() only accepts a single argument, so $queue->createItem($arg1, $arg2); will generate notices if the callback needs more than one argument, (and you loss these extra parameters).

kerasai’s picture

You can always use an array or object containing structured data to pass multiple parameters within the $data parameter. That's what the example with the aggregator module demonstrates.

jdgp77’s picture

Este funcion(el hook) se ejecutara cada vez que se ejecute el cron,

Un ejemplo:
In standar.module


function standar_cron()
{
    //  Este mensaje se registrara cuando se ejecute el cron
    //  Y lo pueden ver en Informes->Mensajes recientes del registro
    watchdog('encron','esta funcion se ejecuto cuando se ejecuto el cron');
}
pferlito’s picture

You could have documented the fact that hook_cron() runs as the anonymous user, instead you left it to be discovered by the developer. Thank you.

Gold’s picture

If cron is being run by drush the drush call can be told to run as any specified user by adding "-u [UID]".

e.g. /path/to/drush -u 1 cron

However, cron can be run in a number of different ways and the developer should either have knowledge of how their code will act (if they have that level of control over the environment it runs in) or target the lowest common denominator (UID 0/Anonymous/or very clear instructions) in the case of contrib modules.

antk’s picture

Hello, when trying to hit my drupal cron url from a windows scheduled task, my site is requiring login. Is this typical?