0

I'm creating my first Magento module, the goal of which is to run some code at specified time invervals (e.g. using the Magento Cron).

However, I'm just trying to keep it simple first of all and add something to the Magento Log so that I know the code is executing when the cron job is run.

At the moment, I'm getting no messages in var/log/system.log when I'm expecting to see a message "Running Ultra Notification Import".

Here is my code:

app/code/community/Trueshopping/UltraNotificationImport/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Trueshopping_UltraNotificationImport>
            <version>0.1.0</version>
        </Trueshopping_UltraNotificationImport>
    </modules>
    <crontab>
        <jobs>
            <ultra_notification_import>
                <schedule><cron_expr>* * * * *</cron_expr></schedule>
                <run>
                    <model>ultranotificationimport/observer::import</model>
                </run>
            </ultra_notification_import>
        </jobs>
    </crontab>
</config>

app/code/community/Trueshopping/UltraNotificationImport/Model/Observer.php

<?php

class Trueshopping_UltraNotificationImport_Model_Observer
{
    /**
     * Import notifications from Ultra
     */
    public function import()
    {
        Mage::log('Running Ultra Notification Import');
    }
}

app/etc/modules/Trueshopping_UltraNotificationImport.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Trueshopping_UltraNotificationImport>
            <active>true</active>
            <codePool>community</codePool>
        </Trueshopping_UltraNotificationImport>
    </modules>
</config>

Thanks!

Anthony

3

You are missing the models declaration in your config.xml file.
It should look like this:

<?xml version="1.0"?>
<config>
    <modules>
        <Trueshopping_UltraNotificationImport>
            <version>0.1.0</version>
        </Trueshopping_UltraNotificationImport>
    </modules>
    <global>
        <models>
             <ultranotificationimport>
                 <class>Trueshopping_UltraNotificationImport_Model</class>
             </ultranotificationimport>
        </models>
    </global>
    <crontab>
        <jobs>
            <ultra_notification_import>
                <schedule><cron_expr>* * * * *</cron_expr></schedule>
                <run>
                    <model>ultranotificationimport/observer::import</model>
                </run>
            </ultra_notification_import>
        </jobs>
    </crontab>
</config>

Clear the cache after you modify the config file.

  • Thanks Marius. I've added that code but I still don't see any output from the cron. Even after manually running /path/to/magento/cron.php I presume I would see the message "Running Ultra Notification Import" in /var/log/system.log but there is no message there. Also all caching is turned off and I logged out and logged in again just incase. – Anthony Jun 17 '14 at 13:32
  • @Anthony The first time you call cron.php the task is not executed. It is planned. Check the table cron_schedule for an entry that contains ultra_notification_import. It is executed the second time you call the cron. But be careful. If you call the cron again too soon it will not run. So clear the cache before running manually each time. Even if the cache is disabled. – Marius Jun 17 '14 at 13:38
  • Hi @Marius, I've cleared the cache and re-ran the cron.php file and then checked the cron_schedule table but I just see this: evernote.com/shard/s15/sh/76cb7521-2ac8-4c6c-856d-00464804dfbf/… – Anthony Jun 17 '14 at 13:58
  • I've tried it again a few times to make sure but still there is nothing in the cron_schedule table. :\ – Anthony Jun 17 '14 at 14:21
1

Make sure your server's cron process is running. Through a secure shell (SSH) you can check this with:

crontab -l

This will list the current cron jobs that the server will run. You should see something like:

*/15 * * * * /home/user/public_html/magento/cron.sh

That simply tells the server you want to run cron.sh (the Magento bash script that runs cron) every 15 minutes. If you do not see a line like that, use:

crontab -e

You can now edit the crontab to add a reference to the Magento bash script like above. Essentially, what will happen is:

  • cron.sh runs and calls cron.php
  • cron.php gathers the cron jobs and adds them to the schedule (cron_schedule table)
  • tasks are now scheduled, but they will not run until the server cron process runs again (in this case, 15 minutes).
  • Server cron runs and checks the cron_schedule to see which tasks should be run according to Magento's schedules.
  • Scheduled tasks are run and the finished_at field in the table will then be set to when the task finished.

During testing (and in a dev environment ideally) you can set the server's cron to just run every minute - */1 * * * * that way you aren't waiting around forever to find out if the jobs have been run.

Also, something to keep in mind are the timestamps associated with tasks. The cron_schedule table will list times in UTC no matter what timezone settings you have set on your server or in Magento. I suggest synchronizing your server time (using NTP or the like) and applying the same timezone you have set in Magento. This way you can schedule cron tasks based on your local time and you won't have to deal with converting to UTC in your head constantly (only for figuring out when tasks in cron_schedule will run).

You may also need to restart the cron service, such as:

sudo service cron restart

--- Note: This is obviously for a Linux server. If you're running on Windows, I assume the process is similar, but I've never worked with a Windows server to this depth, so I don't know the proper steps off hand to set up cron.

  • Hi @pspahn. Thanks for your comprehensive answer but it doesn't seem to be the solution unfortunately. When I run crontab -l I see this line: * * * * * php /var/www/html/base/overlay/anthony-ultra-module/cron.php So this should be running every minute. But when I run the cron.php manually and then check the cron_schedule my module is nowhere to be seen. Here is what I see on the command prompt. evernote.com/shard/s15/sh/76cb7521-2ac8-4c6c-856d-00464804dfbf/… The dev server is running on NGINX and there is an overlay system being used. – Anthony Jun 18 '14 at 8:00
  • Please edit your original to reflect changes suggested by @Marius. – pspahn Jun 18 '14 at 8:05
  • Hi, I've just had a chat to one of our Magento contractors who manages the server and I think my cron job may have been disabled from another layer somewhere. I'm just testing this and will update this question with the outcome. – Anthony Jun 18 '14 at 8:19
  • It seems the main issue was the cron has been disabled on another level by the administrators but I've marked @Marius's answer as the correct answer as he did identify I had a missing model declaration in my config.xml file. – Anthony Jun 18 '14 at 10:14
0
<?xml version="1.0"?>
<config>
<modules>
<Demo_Practice>
<version>1.0.1</version>
</Demo_Practice>
</modules>
<global>
<models>
<practice>
<class>Demo_Practice_Model</class>
</practice>
</models>
</global>
<crontab>
<jobs>
<demo_practice_send>
<schedule><cron_expr>*/5 * * * *</cron_expr></schedule>
<run><model>practice/observer::logmethod</model></run>
</demo_practice_send>
<demo_practice_send1>
<schedule><cron_expr>*/5 * * * *</cron_expr></schedule>
<run><model>practice/observer::method1</model></run>
</demo_practice_send1>
</jobs>
</crontab>
</config>

And function define as

<?php

class Demo_Practice_Model_Observer
{
    public function logmethod()
    {   Mage::log("Voila!!");

     }
    public function method1()
    {   Mage::log("This is a cron run test", null, 'custom.log');
     }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.