4

Hi i am tring to set up cron in magento using config.xml. but anyreason its not working, Here is the code which i am using.

config.xml

<?xml version="1.0"?>
<config>
    <global>
        <models>
            <mycompany_mymodule>
                <class>Mycompany_Mymodule_Model</class>
            </mycompany_mymodule>                         
        </models>
    </global>
    <crontab>
        <jobs>
            <mycompany_mymodule>
                <schedule><cron_expr>*/10 * * * *</cron_expr></schedule>
                <run><model>mycompany_mymodule/observer::importproduct</model></run>
            </mycompany_mymodule>
        </jobs>
    </crontab>
</config>

Observer.php

<?php  
class Mycompany_Mymodule_Model_Observer {  
    public function Importproduct() {
        Mage::log('cron works!!',null,'cron.log');
    }  
}  ?>

but its not working, can anybody help me. Thank you in advance..

Rick Kuipers
  • 4,260
  • 2
  • 20
  • 24
gks
  • 464
  • 4
  • 13
  • Is your cron actually setup? is cron.php being called by your system's cron? Try running cron.php yourself. – Rick Kuipers May 21 '13 at 12:43
  • i added Mage::log('test'); in my cron.php file, When i run www.mydomain.com/cron.php its giving output, but with this midule its not giving any output. – gks May 21 '13 at 12:44
  • Is your module enabled in app/etc/modules/? In other words, did you check whether the module itself is loaded at all? – Rick Kuipers May 21 '13 at 12:50
  • Thanx Rick,Yes i have this code in app/etc/modules... true community – gks May 21 '13 at 12:51
  • Also, in your config.xml you reference this: <run><model>mycompany_mymodule/observer::importproduct</model></run> where you call the function importproduct and in your PHP code it's Importproduct, note the capital. – Rick Kuipers May 21 '13 at 12:53
  • ok..thank you Rick, but still its not working.. please anything else i need to do.. i want to run cron through only magento custom module, so i haven't add cron in cpanel, is that ok? – gks May 21 '13 at 13:00
  • Play with cron jobs, play with 'AOE Scheduler' module: http://www.fabrizio-branca.de/magento-cron-scheduler.html – MagePsycho May 21 '13 at 14:24

2 Answers2

1

An answer with a collection of all the comments:

Is your cron actually setup? is cron.php being called by your system's cron? Try running cron.php yourself.

Is your module enabled in app/etc/modules/? In other words, did you check whether the module itself is loaded at all?

Also, in your config.xml you reference this: mycompany_mymodule/observer::importproduct where you call the function importproduct and in your PHP code it's Importproduct, note the capital I.

Rick Kuipers
  • 4,260
  • 2
  • 20
  • 24
  • i make "importproduct" from "Importproduct" but still not getting any result – gks May 21 '13 at 13:02
  • @Gmb did you clear your var/cache/ after doing that? – Rick Kuipers May 21 '13 at 13:06
  • no, i didn't,, but now i have done it. – gks May 21 '13 at 13:18
  • And running cron.php now still doesn't result in any response? Is your log directory writable for it to write to cron.log? Check the cron_schedule table in your database, does it have entries from your module? – Rick Kuipers May 21 '13 at 13:20
  • I also have added mail function after Mage::log(); in Observer.php, but i am not getting mail. – gks May 21 '13 at 13:22
  • @Gmb Is your module visible in Magento backend System -> Configuration -> Advanced (all the way on the bottom left)? – Rick Kuipers May 21 '13 at 13:24
  • fur running cron using config.xml, this is the sufficient code? or anything more require? – gks May 21 '13 at 13:25
  • @Gmb Yes, I compared it to my config. The config you posted seems to be fine. – Rick Kuipers May 21 '13 at 13:26
  • bad luck for me.. :( – gks May 21 '13 at 13:34
  • @Gmb If my answer isn't your solution then please remove it as an accepted answer by clicking the check mark next to it again. – Rick Kuipers May 21 '13 at 13:37
  • any server configuration is need to change? – gks May 21 '13 at 13:37
  • i accepted your answer because you shown my 1 mistake to remove first capital letter and make it lowercase. – gks May 21 '13 at 13:38
  • @Gmb Server configuration is needed to eventually have the cron.php be executed regularly, for testing purposes executing cron.php is enough. – Rick Kuipers May 21 '13 at 13:39
  • i am not running cron.php manually, i am just using this module to run cron.php.. – gks May 21 '13 at 13:40
  • @Gmb The purpose of cron.php is to run all the crons defind in config.xml. So you can't have it call itself. – Rick Kuipers May 21 '13 at 13:47
  • then how can i run only this cron fuction. i want to run this function every 1 hour, for testing i have added for every 10 minits. please can you suggest me>... Thanx – gks May 21 '13 at 13:51
  • @Gmb You have a couple options, either use a controller action which you call by URL. Or create a file where you load Magento and execute what you want to execute. Although I still don't understand why you don't want to just run cron.php every hour, that will execute your function and perhaps some others as well. – Rick Kuipers May 21 '13 at 13:52
  • yes.. i want to run cron.php every hour, but it need to run through config.xml not from www.mydomain.com/cron.php ..... actually my requirement is that i want to create 1 separate module which will run my cron every hour using config.xml setting.i will not run run it manually. – gks May 22 '13 at 04:34
1

Change the method name same as config.xml file, Like the below.

<?xml version="1.0"?>
<config>
    <global>
        <models>
            <mycompany_mymodule>
                <class>Mycompany_Mymodule_Model</class>
            </mycompany_mymodule>                         
        </models>
    </global>
    <crontab>
        <jobs>
            <mycompany_mymodule>
                <schedule><cron_expr>*/10 * * * *</cron_expr></schedule>
                <run><model>mymodule/observer::importProduct</model></run>
            </mycompany_mymodule>
        </jobs>
    </crontab>
</config>

<?php  
class Mycompany_Mymodule_Model_Observer {  
    public function importProduct() {
        Mage::log('cron works!!',null,'cron.log');
    }  
}  
liyakat
  • 3,995
  • 7
  • 27
  • 35