Magento Cron Example

1. Create the main module config file.
app/etc/modules/Magentotutorial_CronExample.xml

<?xml version="1.0"?>
<config>
  <modules>
    <Magentotutorial_CronExample>
      <active>true</active>
      <codePool>local</codePool>
      <version>0.1.0</version>
    </Magentotutorial_CronExample>
  </modules>
</config>

2. Create the second module config file.
app/code/local/Magentotutorial/CronExample/etc/config.xml

<?xml version="1.0"?>
<config>
  <modules>
    <Magentotutorial_CronExample>
      <version>0.1.0</version>
    </Magentotutorial_CronExample>
  </modules>
  <global>
    <helpers>
      <cronexample>
        <class>Magentotutorial_CronExample_Helper</class>
      </cronexample>
    </helpers>
	<models>
	  <cronexample>
		<class>Magentotutorial_CronExample_Model</class>
		<resourceModel>cronexample_mysql4</resourceModel>
	  </cronexample>
	</models>
  </global>    
    <crontab>
        <jobs>            
			<cronexample_crontest>
                <schedule><cron_expr>*/1 * * * *</cron_expr></schedule>
                <run><model>cronexample/cron::cronTest</model></run>
            </cronexample_crontest>
        </jobs>
    </crontab>
</config> 

3. Create the helper
app/code/local/Magentotutorial/CronExample/Helper/Data.php

<?php
class Magentotutorial_CronExample_Helper_Data extends Mage_Core_Helper_Abstract
{
}

4. Create the php file that does the actual task.
app/code/local/Magentotutorial/CronExample/Model/Cron.php

<?php
class Magentotutorial_CronExample_Model_Cron{	
	public function cronTest(){
		Mage::log("Cron Test ".now(),null,"mageCronExample.log");
	} 
}

5. Assume Magento is hosted on Ubuntu, and local url for magento is http://localhost/
Create a file /etc/cron.d/mageCron with the below line. It will run the cron every 5 minutes.

*/5 * * * * root wget -q http://localhost/cron.php

If we want to run the cron job in magento manually, we can also either issue the command
wget -q http://localhost/cron.php on Ubuntu command line or open a browser and visit
http://localhost/cron.php

Search within Codexpedia

Custom Search

Search the entire web

Custom Search