Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
}
}
?>
Comments
Once a day at given hour
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
Thanks but...
Thanks for this but I think you wanted to write :
David THOMAS
http://www.smol.org
Actually you don't need time
Actually you don't need time as its there by default
<?phpfunction YOURMODULE_cron
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
Thanks really, its what i
Thanks really, its what i need..
How to check weekly ?
Weekly, i want send email to clients. How can i do that ?
weekly schduling
if (date('G')==0 && date('i')==00 && date('l')=="Monday") {
//Your code here
}
* change day according to your requirement
PHP Cron Scheduler
PHP Cron Scheduler might come handy.
Drupal in the Amazon Jungle
variable_set/get
I wouldn't access tables from other modules, especially when
variable_set
andvariable_get
are so easy to use in d7.