Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup a cron job in Magento module?

Tags:

php

xml

magento

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!

like image 336
Vinayak Garg Avatar asked Jun 26 '12 07:06

Vinayak Garg


People also ask

How does Magento cron work?

About the Magento crontabThe Magento crontab is the configuration used to run Magento cron jobs. Magento uses cron tasks that can run with different configurations. The PHP command-line configuration controls the general cron job that reindexes indexers, generates e-mails, generates the sitemap, and so on.

How does Magento 2 cron work?

Magento cron job — is one of the most important Magento 2 features. It helps to configure commands or scripts that systematically run and perform the tasks you intend it to. With the cron job you don't need to manually reindex, generate google sitemaps, send Magento emails, update currency rates etc.

How do I run cron in Magento 1?

use your browser to hit http://yourdomain.com/cron.php or php-cli to execute cron. php in the root of the application.


2 Answers

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

like image 86
Kenny Avatar answered Oct 22 '22 01:10

Kenny


Be sure to add Magento cron.sh file in crontab

crontab -e

*/5 * * * * /bin/sh /path-to-magento/cron.sh
like image 39
Lance Badger Avatar answered Oct 22 '22 02:10

Lance Badger