14 August 2012

How to make sure your cron task runs once per day in Drupal? You can set crontab once per day, but you still might want to run cron manually every now and then. Also you might have to change the cron to more frequent time for some other module. In any case this code makes sure it's run daily.

Source code viewer
  1. /**
  2.  * Implements hook_cron().
  3.  */
  4. function HOOK_cron() {
  5. $last_run = variable_get('HOOK_cron_last_run', 0);
  6.  
  7. if (strtotime('+1 day', $last_run) < REQUEST_TIME) {
  8.  
  9. // your cron task
  10.  
  11. variable_set('HOOK_cron_last_run', REQUEST_TIME);
  12. }
  13. }
Programming Language: PHP