How to schedule a regular job using Rules in Drupal 7

Using Rules to schedule a regularly executed set of actions is actually incredibly simple. It is as simple as creating a Rule that is executed or scheduled in response to an event within Drupal. I''m relatively new to Rules and I failed to find out how to do this by searching the interwebs, so I thought I''d write it down here.

The default tab in Rules ( /admin/config/workflow/rules ) invites you to add reaction rules, i.e. rules that occur in response to an event. If you export one of these rules you will see this line:

 "PLUGIN" : "reaction rule",

You can create rules that are not reaction rules by going to the "Components" tab, adding a new component, and choosing "Rule" for the component type. If you create a rule this way and then export it, you will see this line:

 "PLUGIN" : "rule",

If you don''t have one of these rules yet then create one. Back at the Components tab, execute the new rule in order to test that it does what you want. To go any further, the Rules Scheduler (rules_scheduler) module needs to be enabled. With a working rule, you can now add a new Action to it. The action you want to add is:

Rules scheduler    Schedule component evaluation

You''ll then be asked to select the component to schedule. Choose the same rule that you are modifying. You will be asked for an evaluation date. This allows you to set the frequency of the job, for example, one of these:

 +7 days +1 day +6 hours

(Note: Scheduled jobs are only ever executed when Cron is run. There is no point scheduling a job with a frequency of hours if Cron is only run once a day.) Finally, there is also an "Identifier" field. It is useful but you can leave it blank for now and just save the action. Back on the Components tab, execute the new rule again. Now got to the "Schedule" tab. You should see your rule listed for future execution. When this scheduled rule is executed, it will in turn reschedule the execution of itself, and so on. If this is what you want except that you want the rule to be executed at a different time of day or different day of the week, then delete the scheduled execution of the rule and go back to the Components tab where you can schedule the next execution of the rule for a specific time and date.

Justin Hellings

"Justin? Hell of a guy! We would have kept him but you know how it is. Genius like that is always restless ... Eh? ... Oh, him ... No, I thought you meant someone else."



comments powered by Disqus