Drupal Cron

To write a cron you need to implement hook_cron() in your module. Then, drupal will automatically execute these tasks during the default cron handling.

Note that, using drupal_set_message() function in a hook_cron() implementation makes no sense because there is no user to read these messages. As an alternative, you could log messages with watchdog().

In Drupal 7 you can enable cron via the Administration > Configuration > System > Cron (admin/config/system/cron)

In Drupal 8 you can enable cron via Menu > Configuration > System > Cron (admin/config/system/cron).

The default frequency is every three hours. Cron will then be triggered by end users visiting your site, no more frequently than every three hours.

Note that, for low-traffic sites it can also be desirable to create a cron job(More Info @ https://drupal.org/cron & https://drupal.org/node/23714).

Make a note, 'elysia_cron' is the module which is generally good and used to manage the crons for drupal setup..

Basic default crons by drupal 7 are:

dblog_cron (w) Remove expired log messages and flood control events [runs every hour]

field_cron (w) Purges some deleted Field API data, if any exists [runs every hour]

node_cron (w) History table cleanup [runs every hour]

search_cron (w) Update search database index [runs every hour]

system_cron (w) Remove older rows from flood and batch table. Remove old temporary files. [runs every hour]

update_cron (w) Checks for available updates of Drupal core, contributed modules and themes [runs every hour]

To fire a cron task once a day create a module containing hook_cron() and wrap the code in a condition. Use a system variable to determine whether or not the code has executed for each particular day.

For example:

function mymodule_cron( ) {
  # today's date as of midnight, exp. 04 / 16 / 09 12:00:00 am
  define ( 'TODAY' , mktime ( 0 , 0 , 0 , date ( 'n' ) , date ( 'j' ), date ( 'Y' ) , 0 ) );

  if ( variable_get ( 'daily_cron' , TODAY ) ! = TODAY ) {
    variable_set ( 'daily_cron' , TODAY );
    // do whatever else you need
  }
}

Add new comment