Using Magento config to run cron jobs

The days of using crontab to run cron jobs for your Magento installation are long over. Since Magento Community Edition 1.8 (Enterprise Edition 1.13) cron jobs can now be configured using the Magento configuration.

Initial Setup

Before you can schedule your cron jobs, you have to set up Magento’s cron.php or cron.sh using crontab. This will be the only time you will ever have to use crontab.
Using cron.php:

* * * * * /bin/php /var/www/magento/cron.php

Using cron.sh:

* * * * * /bin/sh /var/www/magento/cron.sh

These examples are setup to continuously check for scheduled cron jobs. You can change how often Magento checks for scheduled cron jobs, just make sure you know what you are doing first.

Scheduling a Cron Job

Now that the Magento cron job is setup, you can start scheduling custom cron jobs. You do this by adding a crontab block to the end of the config.xml file of your module:

<config>
...
...
    <crontab>
        <jobs>
            <my_cron_job>
                <schedule>
                    <cron_expr>0 1 * * *</cron_expr>
                </schedule>
                <run>
                    <model>mymodule/observer::myFunctionName</model>
                </run>
            </my_cron_job>
        </jobs>
    </crontab>
<config>

This particular example will run the myFunctionName() function in the observer.php of the “mymodule” module every morning at 1:00am.

That’s it!

Leave a Reply