How to Set up Cron Job in Magento

In Magento some features require a script to be run periodically. To run a script periodically, most operating systems have a built-in scheduled task called CRON. A CRON is a daemon which keeps running and gets activated at a said interval. It helps store owners to setup up and schedule commands which will run periodically at set time and date. Here is the tutorial for how to set up CRON job in Magento to run periodically.

How to Set up Cron Job in Magento
How to Set up Cron Job in Magento


Following features require a CRON schedule:

  • Catalog Price rules
  • Sending Newsletters
  • Generating Google Sitemaps
  • Customer Alerts/Notifications (product price change, product back to stock)
  • Automatic updating of currency rates
  • Scheduled DB logs cleanup

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.

Magento Cron Best Practices( all times are in minutes )

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 in every 15 minutes for better results and that will not overlap with other Cron services.

Inner workings(Background) of cron

    // 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

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