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

Share

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.

Leave a Reply

You must be logged in to post a comment.