Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I need, every 2 month, to execute some rule component programmatically but I don't know how to do this.

If I use hook_cron I don't know how to check 'once every 2 month' condition.
I can check

'the 10th day of the month' + 'month number is even'

but if the cron is runned twice this day? I cannot send 2 identical emails to users..

How can I achieve this?

share|improve this question
1  
You'll need to write some logic to check the current date/time, against a value you store the last time your cron job ran. That logic isn't Drupal-specific (just basic PHP) so we can't help you with it here; but you'll probably want to use variable_get/variable_set to store/retrieve your timestamp –  Clive Feb 18 at 10:05
1  
Or alternatively run a system cron job (eg 0 0 1 */2 *) possibly calling a php cli script (provided you have shell access). –  Wtower Feb 18 at 10:10

3 Answers 3

up vote 1 down vote accepted

You can easly do this without having access to crontab/shell, by using following code in your cron_hook:

// check if current day is 10th and month is an even number
if (date('d')==10 && date('m') % 2 == 0) {
    // get todays-date (format: yyyymmdd)
    $today = date('Ymd');
    // get info about last run
    $last_run = variable_get('my_last_run', 0);
    // check last run was not today
    if ($last_run!=$today) {
        // set last run to today
        variable_set('my_last_run', $today);

        /* Place your code here */

    }
}

For more info on the modulo-operator (the % ) check:
http://www.php.net/manual/en/internals2.opcodes.mod.php

share|improve this answer
    
Using this method is useful install Elysia Cron –  arrubiu Feb 18 at 11:37
    
Why install a module if you can achieve this with a simple php function? –  Shyghar Feb 18 at 14:44
    
This is not work !!! –  zhilevan Feb 20 at 3:48
    
@shyghar , do u have process that execute every second and check your condtions?!!!!!!!!!!! , This is not correct way ,if you think it work in php , I tell you are mistakes, because php not run any function any second to check your condition available,if you put it in default drupal raw, it run when only cron run .by the way this is completely mistake method !!!! –  zhilevan Feb 20 at 3:52
    
Nope.. I inserted this code in a hook_cron() so this code will be executed only with mt drupal cron [once a day].. –  Shyghar Feb 20 at 10:44

Rules has a submodule called rules_scheduler made for that purpose.

share|improve this answer

The best way is use crontab ,create a php file as example mycorn.php trigger your rule in it and create a crontab to run your php file every 2 month.
Just one note, please set permission only owner can execute it.

Below links help you , how to set cron.

http://www.adminschoice.com/crontab-quick-reference/
http://www.thegeekstuff.com/2009/06/15-practical-crontab-examples/

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.