How to set up a Magento cron task?
Piotrek KaczkowskiReading Time: < 1 minute
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.