How to Set Up a Cron Job

I DONT UNDERSTAND THIS EXPLANATION, AS DO MANY step 1, how do you schedule a cronjob correctly to work with Magento? step 2, how do I add programs to the schedule? (and what is the relation with schedules in the backend versus config.xml) step 3, where can I find logs or other to control whats happening

A few features in Magento require a script to be run periodically. These features include, but are not limited to:

  • 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

To run a script periodically, most operating systems have a built-in scheduled task spawning mechanism service (see OS-specific instructions below).

Magento and crontab

Magento allows you to schedule custom tasks in an XML configuration, in a similar manner to the UNIX crontab style. Here is an example from app/code/core/Mage/CatalogRule/etc/config.xml:

  1. <config>
  2.     <crontab>
  3.         <jobs>
  4.             <catalogrule_apply_all>
  5.                 <schedule><cron_expr>0 1 * * *</cron_expr></schedule>
  6.                 <run><model>catalogrule/observer::dailyCatalogUpdate</model></run>
  7.             </catalogrule_apply_all>
  8.         </jobs>
  9.     </crontab>
  10. </config>

This example will run Mage_CatalogRule_Model_Observer::dailyCatalogUpdate method every day at 01:00am (0 1 * * *).

You can extend this functionality in your own custom modules. To utilize this, inside of your module’s config.xml add the following:

  1. <config>
  2.     <crontab>
  3.         <jobs>
  4.             <namespace_module>
  5.                 <schedule>
  6.                     <cron_expr>0,15,30,45 * * * *</cron_expr>
  7.                 </schedule>
  8.                 <run>
  9.                     <model>module/model::method</model>
  10.                 </run>
  11.             </namespace_module>
  12.         </jobs>
  13.     </crontab>
  14. </config>

That will run every 15 minutes on the quarter hour.

To execute all these configured tasks, the cron.php file located in the Magento root will need to be run periodically, for example every 15 minutes. Basically, this script will check if it needs to run any tasks, and if it needs to schedule any future tasks.

In UNIX/BSD/linux systems you will need to add this line (or a similar line) to your crontab:

  1. # for debugging purposes only:
  3. */5 * * * *  /bin/sh /absolute/path/to/magento/
  4. # /absolute/path/to/bin/php – replace with path to your PHP5 CLI executable
  5. # example: /usr/local/php5/bin/php-cli
  6. # in order to find what is the PHP CLI executable you can try to the following command in shell:
  7. # which php
  8. # /absolute/path/to/magento/cron.php – replace with path to Magento installation
  9. # example: /home/user/public_html/magento/cron.php

If you can’t get the above to work your version of crontab might not be supporting the */5 syntax. Try this instead:

  1. 0,5,10,15,20,25,30,35,40,45,50,55 * * * * /bin/sh /absolute/path/to/magento/

Magento provides a shell script in the magento root: However, this might not work for your system. The below script works for me on Solaris. Some changes I’ve made * != turn into -ne * if a negated if has to be rewritten by using else with an empty then statement * expr index did not work so i rewrote it using awk * you have to change the user from www to whatever your user is in the ps statement (in my case it’s webservd) * my ps does not support the aux format

  1. #!/bin/sh
  2. # location of the php binary
  3. if [ “$1” -ne “” ] ; then
  5. else
  6. CRONSCRIPT=cron.php
  7. fi
  8. PHP_BIN=`which php`
  9. # absolute path to magento installation
  10. INSTALLDIR=`echo $0 | sed ‘s/’`
  11. #    prepend the intallation path if not given an absolute path
  12. if [ “$INSTALLDIR” -ne “” -a “`echo $CRONSCRIPT | awk ‘{ print substr( $0, 0, 1 ) }’`” -ne “/” ];then
  13.     if ps -au webservd | grep “$INSTALLDIR””$CRONSCRIPT” | grep -v grep 1>/dev/null 2>/dev/null; then
  14.     :;else
  16.     fi
  17. else
  18.     if ps -au webservd | grep ” $CRONSCRIPT” | grep -v grep | grep -v 1>/dev/null 2>/dev/null; then
  19.     :;else
  20.         $PHP_BIN $CRONSCRIPT &
  21.     fi
  22. fi


UNIX/BSD/Linux systems have a crontab service. Crontabs are edited for each user using command crontab -e. (If you are logged in as root and want to edit a specific user’s crontab, you can use the command crontab -u user -e.) This will open an editor. It is possible that the contents will be empty if you have never set up crontabs for the current user.

The syntax for crontab file is:

  1. # all the comment lines will start with ‘#’ character.
  2. # in the beginning of the file set environment variables to be used in executed tasks:
  3. # ENV_VAR=”value”
  4. # the following line will send output for executed tasks to specified email:
  6. # declare running tasks in the following pattern:
  7. # <minute> <hour> <day-of-month> <month> <day-of-week> <command>
  8. # this example will run /home/user/ every minute:
  9. * * * * * /home/user/
  10. # this example will run /home/user/
  11. # every 15 minutes for 3 hours after midnight on weekdays during summer months:
  12. */15 0-2 * 6-8 1-5 /home/user/

As you can see the syntax is very flexible. For more information visit the following link:

If for some reason you can’t locate or access your php binary via crontab, you can also use curl or wget to activate cron.php

  1. */5 * * * * curl -s -o /dev/null


Windows has a Scheduled Tasks service which is accessible from the Control Panel.

Other solutions

If you do not have access to crontab on your service, you can set up the page that needs to be run periodically as a Home Page in you personal computer browser (http://yourdomain/yourmagentofolder/cron.php). Every time you open a new window or tab, it will execute the scheduled task(s) on your server.

You could also set up a Scheduled Task on a computer to which you have access and which is usually running. It could then access a web accessible page that will run a cron job. In UNIX/BSD/Linux it can be done with wget or curl utilities.

Lastly, if either of these won’t work for you, there are a number of online cron services (complete list: that may be useful. Many are free, but have restrictions on execution frequency or job count. Two reliable English services tested are: and

Inner workings

Magento crontab mechanism is triggered periodically using system cron job outlined above.

The call is initiated in cron.php file:

  1. <?php
  2. // initialize configuration and load event observers only from /crontab/ section
  3. Mage::getConfig()->init()->loadEventObservers(‘crontab’);
  4. // initialize crontab event area
  5. Mage::app()->addEventArea(‘crontab’);
  6. // dispatch ‘default’ event for observers specified in crontab configuration
  7. 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.

Error logging

Errors are logged in cron_schedule. If the status column for a scheduled job is ‘error’ then you will see the normal stack trace that magento provides in the messages column.


You can fine tune execution and scheduling of Magento cron jobs in Admin > System > Configuration > System > Cron

  • Generate schedules every [A] (minutes)

New schedules will be generated not more frequently than A minutes.

  • Schedule ahead for [B] (minutes)

New schedules will be generated for B minutes ahead.

  • Missed if not run within [C] (minutes)

If cron.php was executed within C minutes after the task was scheduled, it will be executed. Otherwise it will be marked as missed

  • History cleanup every [D] (minutes)

History cleanup will happen not more frequently than D minutes.

  • Success history lifetime [E] (minutes)

Successfully finished scheduled tasks will remain in database table for E minutes.

  • Failure history lifetime [F] (minutes]

Failed and missed scheduled tasks will remain in database table for F minutes.

Built-in Cron Jobs

The following cronjobs come along with your Magento install and can be found in each module’s config.xml.


Module Cronjob Cron syntax Frequency
CatalogIndex reindexAll 0 2 * * * Daily at 02:00am
CatalogIndex runQueuedIndexing * * * * * Every time cron is run
CatalogRule dailyCatalogUpdate 0 1 * * * Daily at 01:00am
Directory scheduledUpdateCurrencyRates Admin→System→Configuration→Currency Setup
Log logClean */10 * * * * Every 10 minutes
Newsletter scheduledSend * * * * * * Every time cron is run, also see Newsletter settings
ProductAlert Admin→System→Configuration→Catalog
Sales cleanExpiredQuotes 0 0 * * * Daily at midnight
Sitemap scheduledGenerateSitemap Admin→System→Configuration→Google Sitemap

All Settings are in Admin→System→Configuration. Some jobs are commented out in the code as indicated in the last column.

Module Cronjob Cron Syntax Frequency Left out
Log log/cron:: logClean System→Log Cleaning
log/aggregation:: run */10 * * * * Every 10 minutes Yes
Tax tax/observer:: aggregateSalesReportTaxData 0 0 * * * Daily at midnight
CatalogRule catalogrule/observer:: dailyCatalogUpdate 0 1 * * * Daily at 1 am
Sales sales/observer:: cleanExpiredQuotes 0 0 * * * Daily at midnight
sales/observer:: aggregateSalesReportOrderData 0 0 * * * Daily at midnight
sales/observer:: aggregateSalesReportShipmentData 0 0 * * * Daily at midnight
sales/observer:: aggregateSalesReportInvoicedData 0 0 * * * Daily at midnight
sales/observer:: aggregateSalesReportRefundedData 0 0 * * * Daily at midnight
Sitemap sitemap/observer:: scheduledGenerateSitemaps Google Sitemap→Generation Settings
Directory directory/observer:: scheduledUpdateCurrencyRates Currency Setup→Scheduled Import Settings
CatalogIndex catalogindex/observer:: reindexAll 0 2 * * * Daily at 2 am Yes
catalogindex/observer:: runQueuedIndexing * * * * * Every time cron is run Yes
ProductAlert productalert/observer:: process Catalog→Product Alerts Run Settings
Catalog catalog/product_indexer_price:: reindexAll 0 2 * * * Daily at 2 am
SalesRule salesrule/observer:: aggregateSalesReportCouponsData 0 0 * * * Daily at midnight
Newsletter newsletter/observer:: scheduledSend */5 * * * * Every 5 minutes