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

// initialize configuration and load event observers only from /crontab/ section
// initialize crontab event area
// dispatch 'default' event for observers specified in crontab configuration

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 ?

  • 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

    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

  • 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.

  • 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 і навіщо він потрібен |

  • 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 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 * * *

    whts wrong pls help

  • Swapna Silu

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

    */1 * * * *


    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

  • 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 (, 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?

  • Sanjay Raut

    Thanks for tutor.
    I have set the cron to execute it every 30 min.

    */30 * * * *


    Also I have configure best practise for cron in admin.
    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

    In Cron_schedule table sometimes schedule get generated for release_get_pdf_nfs_process cron, and sometime not.
    Because of this my cron is not executed at given time internal ie every 30 min.
    I dont understand the reason behind it.
    Could you please help me.

  • Djo

    Hi, getting this kind of error after a server migration,
    Error: Skipping oversized log recordWarning: Truncating oversized referrer field [311]
    Any ideas ?

  • ChefMaha

    Hi and thanks for the illustrative article!

    I have been noticing lately that my extensions that do automatic stuff (like sending emails or SMS) are not doing what they’re supposed to do. I figure there may be an issue with my cron job. How do I know whether my cron is configured correctly for these extensions?

  • Josh

    Im having issues with magento store I recently install extention to see what cron jobs I have running on my site. But for some reason the heartbeat cron job doesnt run at all, the cron.php / files are executable (744) but its doesnt seem to be running.

  • Babbage

    I use wich is an affordable online cronjob service. Advantage for me is that I then have an overview of all scheduled tasks, on all servers.

  • jay

    Thanks for good tutorial on how to set up cron on magento.
    If anyone want to setup their own cron job in their own module here’s good stuff

  • Quang Hiếu Phạm

    Thanks for sharing an useful post like this. I will comeback and read magento beginner’s guide again.

  • Guillaume

    Hi !
    Thank you for the explanations.
    I made a module in which I have a script that have to be run daily. I configured it in my etc/config.xml.
    When I launch the cron.php for the first time, I have a few lines in table cron_schedule taht correspond to my module. The status is pending and executed_at is null.
    The second time I launch cron.php, the table cron_schedule is updated with executed_at at now() but the status is still pending.
    The third time cron.php is launched (I set it up for every 5 minutes), my script is still running (it lasts around 8 minutes) but the status in the database is still pending, so the system tries to run it again. And it raises a database error.

    How could the status not be updated when the script is run by cron ?

    Thank you by advance

  • Goran Tomic

    This will run at 01:00 every day, not every hour. If you want to make it every hour then you need to do it like this 0 * * * *.