Create Cron Jobs in Magento

Set up a cron job in magento module

Posted on March 15, 2016 in Magento, PHP

In this example, I use My_Module as an example to show how to add cron job in Magento.

First let magento know about the cron job in config.xml, shown as below:


<config>
    <modules>
        <My_Module>
            <version>0.1.0</version>
        </My_Module>
    </modules>
    <global>
       <models>
           <my_module>
               <class>My_Module_Model</class>
           </my_module>
       </models>
    </global>
    <crontab>
        <jobs>
            <mymodule_setstatus>
                <schedule><cront_expr>* 17 * * *</cron_expr></schedule>
                <run><model>my_module/observer::setStatus</model></run>
            </my_module_setstatus>
        </jobs>
    </crontab>
</config>

We will set up setStatus() method in app/code/local/My/Module/Model/Observer.php run at 5:00PM every day.

Next we add setStatus() method as following:


#app/code/local/My/Module/Model/Observer.php

class My_Module_Model_Observer 
{
    public function setStatus()
    {
        Mage::log('Works!');
    }
}

You will see string “Works!” adding to the system.log at 5:00PM everyday.


comments powered by Disqus