1

In custom module's configuration, we allow admin to select a day when a task needs to be executed.

How do I specify that value dynamically in cron schedule?

for eg: Admin selected 20 in the settings page, then I need the cron schedule like this: * * 20 * *

Please let me know if anyone have achieved this before?

2 Answers 2

3

You have to set the cron to minimum reasonable interval (e.g. each 5 minutes) and then check inside of your module if the job has to be executed according to config settings.

2
  • So, there isn't a way to dynamically set the cron, right? Anyways, your solution is nice and simple. I will set my cron to run that task every day and if the date is same as selected by Admin, then I will perform the desired task. Cool tip. Thanks Tim :) Feb 25, 2014 at 9:53
  • 1
    Checking inside your module means you get to handle all the fun bits of date/time comparison. Not too mention duplicating the same system you're hooking into. The better approach is just write out the change to the modules config.xml nodes. svn.magentocommerce.com/source/branches/1.8/app/code/core/Mage/… in particular generate() function.
    – B00MER
    Feb 25, 2014 at 10:13
1

In your modules config.xml put the following:

<config>
  <global>
      <models>
          <roomstoryinvoicecron>
              <class>Roomstory_Invoice_Model</class>
          </roomstoryinvoicecron>                         
      </models>
  </global>
  <crontab>
      <jobs>
          <roomstoryinvoicecron>
              <schedule><cron_expr>*/10 * * * *</cron_expr></schedule>
              <run><model>roomstoryinvoicecron/observer::setStatus</model></run>
          </roomstoryinvoicecron>
      </jobs>
  </crontab>
</config>

In app/code/local/Roomstory/Invoice/Model/Observer.php add the following:

<?php
class Roomstory_Invoice_Model_Observer {
    public function setStatus() {
      Mage::log("WORKS!");
    }
}

Make sure logging is enabled and it should work, check the log to be sure ;)

Source: - https://stackoverflow.com/questions/11202463/how-to-setup-a-cron-job-in-magento-module

1
  • 1
    Thanks boomer, however, I wanted to know how to set cron dynamically. In your example you have set cron to run every 10 minutes. How can you make it to run every X day of month. X will be specified by Admin in config settings. Feb 25, 2014 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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