Blog

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:
-
execute scheduled tasks
-
generate future scheduled tasks if needed
-
clean up history of scheduled tasks
Tasks are scheduled for each time the job needs to be run based on
-
<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 ofpending, 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.
View Comments to “How To Setup A Cron Job In Magento – Magento Cron Tutorial”
Leave a Reply
- Recent Posts
- September 2010 (1)
- July 2010 (8)
- June 2010 (1)
- March 2010 (1)
- February 2010 (2)
- January 2010 (10)
- November 2009 (1)
- October 2009 (1)
- September 2009 (3)
- August 2009 (5)


June 21st, 2010 at 8:10 AM
[...] How To Setup A Cron Job In Magento – Magento Cron Tutorial … [...]
August 12th, 2010 at 2:22 PM
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
August 13th, 2010 at 8:51 AM
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?
September 9th, 2010 at 11:12 AM
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.