Jul 21, 2012
kalpesh

Magento: How to run/set cron in Magento

Ever came with the requirement where you need to set cron for periodically running certain jobs automatically? If you are new to cron in Magento, this blog post is going to help you out. Cron is a time-based job scheduler in Unix-like computer operating systems.

There are basically two crons: one is system’s cron (Unix, where it will trigger the specified file, for each specified time (in terms of minute, hour, day, week, year) AND another is Magento’s internal cron where it will decide which file and method to call to complete the operation.

– First, you need to add following snipped in your module’s config.xml file, inside node:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<crontab>
       <jobs>
            <cron_name_must_be_unique>
                <schedule>
                    <!-- This runs cron job daily at 12am mid-night -->
                    <cron_expr>0 0 * * *</cron_expr>
                </schedule>
                <run>
                    <!-- The below module's model file's method name is going to be called each time cron is run -->
                    <model>modulename/modelname::methodname</model>
                </run>
            </cron_name_must_be_unique>
        </jobs>
</crontab>

– Now open crontab of your system, by typing this in terminal:
crontab -l
This will list the cron defined in system, check if there is already cron set up for Magento, you don’t need to do anything here.
Else, type this in terminal:
crontab -e
and add the below line at the end of file:
*/5 * * * * /bin/sh /var/www/magento/cron.sh
(this will run system’s cron every 5 minutes, you can change it as per your need)

2 Comments

  • Hi Kalpesh,

    We have used your code for set up cron job by module but this is not working with my module , may be we have done something wrong, can you please help us. Below is code we have added for setup cron job.

    1.0.0

    Roor_Test_Block

    Roor_Test_Helper

    Roor_Test_Model

    singleton
    Roor_Test_Model_Product_Observer
    addproduct

    */5 * * * *
    test/etest::testCronjob

    Thanks,
    Jack

  • Thanks for sharing this post, i was looking for help in fixing cron job for weekly email setup, https://www.cloudways.com/blog/setup-magento-cron-job/, this post also helped me to configure it.

Leave a comment

 

Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!

Certifications

Honor

Recognition

Magento top 50 contributors

Magento top 50 contributors

Contributions