17

I wanted to setup a cron job inside my module. I followed the instructions on Magento wiki - how_to_setup_a_cron_job, but my cron job is simply not executing.

This is my config.xml (app/code/local/Roomstory/Invoice/etc/config.xml)

<?xml version="1.0"?>
<config>    
    <modules>
        <Roomstory_Invoice>
            <version>0.1.1</version>
        </Roomstory_Invoice>
    </modules>
<!-- -->
    <crontab>
        <jobs>
            <roomstoryinvoice_setstatus>
                <schedule><cron_expr>*/10 * * * *</cron_expr></schedule>
                <run><model>roomstory_invoice/setstatus::run</model></run>
            </roomstoryinvoice_setstatus>
        </jobs>
    </crontab>
</config>

And this is my class. (app/code/local/Roomstory/Invoice/Model/Setstatus.php)

<?php
class Roomstory_Invoice_Model_Setstatus {

  public function run() {
    return true;
  }

}
?>

I have installed a Cron Scheduler Module, which shows my cron job listed, but when I try to "run now" (for debugging), I get error -

Invalid callback: roomstory_invoice/setstatus::run does not exist

This something simple, after much trying, I am still not able to find the error. Please tell some other way to do it, or indicate the error in this code.

Thanks!

Vinayak Garg
  • 6,518
  • 10
  • 53
  • 80

5 Answers5

34

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 ;)

7ochem
  • 2,183
  • 1
  • 34
  • 42
Kenny
  • 5,350
  • 7
  • 29
  • 43
  • I am still getting the same error-`Invalid callback: roomstoryinvoicecron/observer::setStatus does not exist` – Vinayak Garg Jun 26 '12 at 07:43
  • Hi, I have tried using the same code but it's not working for me. Can you tell me what else needs to be set apart from the config and the observer? – jdhaar Aug 05 '13 at 06:21
  • See if the module appears in System -> Configuration -> Advanced. (If not, make sure the module is added in /app/etc/modules/ – Kenny Aug 22 '13 at 14:32
  • Cron not calling automatically. When i do localhost/magento/cron.php then it will call. i have schedule the cron same like you . can you please tell what i m missing.?? – UnderGround Sep 30 '13 at 05:54
8

Be sure to add Magento cron.sh file in crontab

crontab -e

*/5 * * * * /bin/sh /path-to-magento/cron.sh
Lance Badger
  • 791
  • 8
  • 13
2
 <crontab>
        <jobs>
            <CompanyName_ModuleName>
                <schedule>
                    <cron_expr>*/5 * * * *</cron_expr>
                </schedule>
                <run>
                    <model>ModuleName/observer::setStatus</model>
                </run>
            </CompanyName_ModuleName>
        </jobs>
    </crontab>

and create Observer.php file in Model with

    class CompanyName_ModuleName_Model_Observer extends Mage_Core_Model_Abstract
{

   public function setStatus()
   { 

   }
}
Naresh Tank
  • 1,569
  • 10
  • 23
1

You can easily create a module for cron job just follow the following steps:

Create Config.xml file and set cron job in it.

<?xml version="1.0"?>
<config>    
<crontab>
        <jobs>
            <Namespace_Module>
                <schedule>
                    <cron_expr>* * * * *</cron_expr>
                </schedule>
                <run>
                    <model>module/observer::method</model>
                </run>
            </Namespace_Module>
        </jobs>
    </crontab>
</config>

Your observer method:

  class CompanyName_ModuleName_Model_Observer extends Mage_Core_Model_Abstract
{

   public function setStatus()
   { 
//your action
   }
}

now last step go to your hosting cpanel and set path and run time of cron.php file in cron job section

by default you can set path like php -f /home/mercodec/public_html/cron.php in magento.

Deepak Mankotia
  • 4,404
  • 6
  • 30
  • 55
0

before that you have to run this script in your terminal. For ubuntu:*/1 * * * * /usr/bin/php /var/www/html/modulename/cron.php > /dev/null

Jeeva Chezhiyan
  • 218
  • 1
  • 9