Red alarm clock with white cross

How to set up a Magento cron task?

Magento cronjob is responsible for sending newsletters, updating catalogue price rules and much more.
It can be also used for scheduling tasks within custom modules.

One thing about Magento is that it makes a lot of things quick and easy.
Let's say we want to define a cron job, all you need to do is this:

First, define it in your config file:
app/code/local/Kiwee/MyModule/etc/config.xml

<config>
	<crontab>
        <jobs>
            <mymodule_backup><!-- this is a cron job unique identifier -->
                <schedule>
                	<cron_expr>*/5 * * * *</cron_expr><!-- this is cron timing expression, here set to run every 5 mins -->
                </schedule>
                <run>
                	<model>mymodule/backup::runBackup</model><!-- and here we have the method to run [module]/[model]::[method] -->
                </run>
            </mymodule_backup>
        </jobs>
    </crontab>
</config>

Now you just have to create this method in model file you've set above:

class Kiwee_MyModule_Model_Backup extends Mage_Core_Model_Abstract 
{
	protected function _construct() {
		$this->_init('mymodule/backup');
	}
	
	public function runBackup() {
		// your code here
	}
}

And that's it.

FacebookTwitterPinterest

Piotr Kaczkowski

Senior Full Stack Developer

My adventure with computers started when my parents bought our first PC, I was about 10 at the time. At first I was just playing games but after a while I became interested in programming. Starting with Turbo Pascal and Assembler then moved to C++ and PHP.