Last updated June 14, 2010. Created on June 14, 2010.
Edited by lefnire. Log in to edit this page.

With Linux crontabs, you can schedule events to run once per day, month, week, etc. In Drupal, however, your hook_cron() will be run with every call to cron.php. Hopefully a cron-scheduling API is in the works, but in the meantime here's a snippet for scheduling your own hook_cron() jobs.

<?php
function YOURMODULE_cron(){
  $cron_last = db_result(db_query('select timestamp from {watchdog} where type="cron" order by timestamp desc limit 1')); 
  // Or using poormanscron.module:
  // $cron_last = variable_get('cron_last', time());

  // if( date('ym', $cron_last) != date('ym', time() ) ){ // Once per month
  if( date('ymd', $cron_last) != date('ymd', time() ) ){ // Once daily
    // Code here
  }
}
?>

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

Alex Andrascu’s picture

If you have a site where cron runs hourly and you want to run you own module cron at let's say 19'o clock each day you'll do something like

<?php
function YOURMODULE_cron(){
  if( date('G', $time) == 19) ){ // At 19'o clock
    // Code here
  }
}
?>
anou’s picture

Thanks for this but I think you wanted to write :

<?php
function YOURMODULE_cron(){
  if( date('G', time()) == 19) ){ // At 19'o clock
    // Code here
  }
}
?>

David THOMAS
http://www.smol.org

darrenwh’s picture

Actually you don't need time as its there by default

function YOURMODULE_cron(){
  if( date('G') == 19) ){ // At 19'o clock
    // Code here
  }
}
Anybody’s picture

function YOURMODULE_cron(){
  if( date('G') == 19){ // At 19'o clock
    // Code here
  }
}

is right.
There was one ")" too much.

http://www.DROWL.de || Professionelle Drupal Lösungen aus Ostwestfalen-Lippe (OWL)
http://www.webks.de || webks: websolutions kept simple - Webbasierte Lösungen die einfach überzeugen!
http://www.drupal-theming.com || Individuelle Responsive Themes

gopal6988’s picture

Thanks really, its what i need..

tannguyenhn’s picture

Weekly, i want send email to clients. How can i do that ?

vipul tulse’s picture

if (date('G')==0 && date('i')==00 && date('l')=="Monday") {
//Your code here
}

* change day according to your requirement