Drupal Answers is a question and answer site for Drupal developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need, every 2 months, 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 2 months are passed from the last time it was executed.

I can check 'the 10th day of the month' + 'month number is even'.

If cron tasks run twice a day, I cannot send 2 identical emails to users.

How can I achieve this?

share|improve this question

closed as too broad by kiamlaluno May 22 at 19:10

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

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 '14 at 10:05
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 '14 at 10:10
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:

share|improve this answer
Using this method is useful install Elysia Cron – arrubiu Feb 18 '14 at 11:37
Why install a module if you can achieve this with a simple php function? – Shyghar Feb 18 '14 at 14:44
This is not work !!! – zhilevan Feb 20 '14 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 '14 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 '14 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.


share|improve this answer

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