How To Setup A Cron Job In Magento – Magento Cron Tutorial

A CRON is a daemon which keeps running and gets activated at a said interval (time period). Cron helps store owners to setup up and schedule commands which will run periodically at set time and date. Various Magento services such as “Sending Newsletter “, Cleanup with logs, Cleanup cache, Customer notification etc. requires a mechanism where store owners can setup and schedule commands to run periodically.

Most of the web hosts have their own set of task scheduler; you can ask and verify the task scheduler from your webhost. While setting a cron-job make sure that minimum time duration is set to run every 15 minutes (not less than this) as  Magento spends around 1 minute to index 1000 products and sets the status to “Success” from “Pending”.

Now to setup Crontab in Magento go to Admin => system => configuration => system => ‘Cron (Scheduled Tasks)’ tab.

You will notice that all times are in minutes.

Magento Cron Best Practices

Generate schedules every: 60
Schedule ahead for: 1
Missed if not run within: 60
History cleanup every: 120
Success history lifetime: 120
Failure history lifetime: 120

run the cron every 15 minutes for better results and that will not overlap with other Cron services.

What goes in the background

When you set the Cron this will happen

1
2
3
4
5
6
7
8
// initialize configuration and load event observers only from /crontab/ section
Mage::getConfig()->init()->loadEventObservers('crontab');
 
// initialize crontab event area
Mage::app()->addEventArea('crontab');
 
// dispatch 'default' event for observers specified in crontab configuration
Mage::dispatchEvent('default');

This sequence will invoke Mage_Cron_Model_Observer→dispatch(), which in turn will:

  1. execute scheduled tasks
  2. generate future scheduled tasks if needed
  3. clean up history of scheduled tasks

Tasks are scheduled for each time the job needs to be run based on

  1. <schedule><cron_expr>0 1 * * *</cron_expr></schedule>

expression and stored in cron_schedule table.

Each record consists of the following fields:

  • schedule_id – unique identifier for scheduled task
  • job_code – job identifier from configuration
  • status – can be one of pending, running, success, missed, error
  • messages – custom text reported by method that was executed by the job
  • created_at – date/time when the task was created at
  • scheduled_at – date/time when the task is planned to be executed
  • executed_at – date/time when the task was actually executed (null prior to execution)
  • finished_at – date/time when the task finished execution (null prior to execution)

When schedules are generated, status is set to pending, created_at to now() and scheduled_at to target date/time.

When pending schedules are executed, status is set to running and executed_at to now().

When scheduled task is finished successfully, status is set to success and finished_at to now().

When scheduled task has thrown an exception, status is set to error and finished_at to now().

If task status is pending and scheduled_at is older than “Missed if not run within” configured value, status is set to missed.

Please leave me a comment and let me know if you have any problems setting up cron in Magento. Subscribe our RSS to receive latest updates.

  • Pingback: How To: Set-Up Windows 7 XP Mode | Rainy Technology

  • Dawa

    OK but my cron still executes in UTC time but I’m in EUrope/Paris time.
    the localtime on my server is CEST (UTC+2)
    ?? can you explian why ?
    Thx

  • http://www.magentomagik.com/ MagentoMagik

    Double check your server local time as your CRON is entirely dependent on server time along with your Store defaults. What is the country region you have selected for your store?

  • Avinash

    hi
    i set all the given rule but i don’t get mail.
    i am useing aheadwork subscription recurring module for recurring payment but cron is not work that is tha main problem of our site how we set cron job pls let me know.

  • Anonymous

    I think you will need to edit your crontab and add following line:

    */5 * * * * /absolute/path/to/php5 -f /absolute/path/to/magento/cron.php

    */5 means that magento’s cron processor will be invoked every 5 minutes. You can change this value as per your requirement.

  • dawa

    Sorry for the delay …
    Country default : France
    Timezone: Europe/Paris
    as the server in CEST …
    Hmmmm I don’t understand why… Grrrr

  • http://www.magentomagik.com/ Ashish @Magento Themes

    When you setup CRON, regardless of your country settings in your application (in our case Magento); CRON file will be executed as per the server time which might be hosted in USA or somewhere else. You need to ask your hosting company and discuss this issue.

  • http://www.3.com Kristan Lerper

    Superb products from you finding out, man. I have comprehend ones stuff earlier to make sure you therefore you are only also fantastic. My partner and i definitely want exactly what you have received here, genuinely love so what you may be telling along with the means through which an individual declare the idea. You ensure it is fun and you nevertheless treatment meant for to keep them smart. As i can not wait to help read through significantly even more from you. It is literally a new excellent internet site..

  • Pingback: Що таке Cron і навіщо він потрібен | maque.org.ua

  • Emilshamloo

    Hi, and thanks for the tutorial. I am new at Magento. I want Magento update my sitemap every day. I found in this site http://www.magentocommerce.com/wiki/3_-_store_setup_and_management/seo/creating_google_sitemaps_for_magento_sites that I have to set up a cron job to do this. Can you make a tutorial for setting up a cron job for updating sitemap?
    Best regards

  • Vishal Porwal

    my root cron.php is not executing my modules observer. I set config.xml of my module

           
               
                   
                        * 1 * * *
                   
                   
                        termspayment/observer::sendPaymentReminderMail
                   
               
           
          

    whts wrong pls help

  • Swapna Silu

    Hi,
    I created a cronjob in my custom module below is the code in config.xml:-

    */1 * * * *

    subscription/observer::exportProducts

    then i defined the job in model/observer.php file like this:-
    public function exportProducts()
    {
    echo “hello”;
    $resource = Mage::getSingleton(‘core/resource’);
    $conn = $resource->getConnection(‘magentostage’);
    $results = $conn->query(“insert into bbb(title) values(‘test1′)”);
    }

    My job is not getting register in cron_scheduler also it is not
    working.Where i am missing steps,i don’t know.Please Suggest me any
    idea.
    Thanks,
    Swapna

  • Aneeq Tariq
  • Anonymous

    While setting the cron in magento tab will define the cron execution time. In your case you set it to 0 1 * * * so this cron will be executed every day at 01:00 AM. 
    The important thing: You have to set cron execution on the server (www.yourstore.com/cron.php), without it cron will not wok.

    For testing you can set   to  * * * * *  This cron will executed continuously.

  • jin

    Hi Magik_Tejas.
    I have a problem with everyday cron job. If I set expression to every X minutes (*/15 * * * *) – it works. But if I set hours to expression (0 1 * * *) it’s not working. Any suggestions?