Magento Tutorials Magento Cron Jobs

« Back to Magento Tutorials

What is Cron?

To quote Wikipedia, 'cron is a time-based job scheduler in Unix-like operating systems.' It functions similarly to Windows Task Scheduler in that it allows you to specify actions to be ran after a certain interval. This can be useful for running daily backups or deleting your logs at the end of every month.

Where is Cron Used in Magento?

By default, cron is used in Magento for various things such as keeping your catalog indexes up to date and refreshing catalog price rules. Cron jobs can also be set up for custom modules, which we will cover shortly.

Does Magento Need Cron?

In my opinion, all Magento eCommerce installations should have a cron job setup, however, technically, depending on the features your stores offers, you might be okay with out them. The major benefit to setting up a cron job is that it will perform necessary actions automatically for you. This means that if you don't use a Magento cron job, you are likely to have to perform these tasks manually. For example, without a Magento cron job set-up, you will have to manually refresh the catalog price rules and catalog indexes several times a day. If you have a large store this can take a long time and forgetting to do it could lose you money!

Setting Up Magento's Cron Job

Firstly you will need to open your cron tab, which is where you enter your cron commands.

Opening the Cron Tab

To open the cron tab as the user you are logged in as type:

crontab -e

To open the cron tab as a specific user, type:

crontab -u USERNAME -e

In the above example you will to change USERNAME with the username you want to run the cron job as.

Now that you know where to add your cron job command, let's go through two simple ways to set it up.

Magento Cron Job Using WGET

*/5 * * * * wget -q http://www.yourdomain.com/cron.php

In the above example you will need to change the link to point to the cron.php file in your Magento root.

Magento Cron Job Using PHP

*/5 * * * * /path/to/php -f /local/path/to/cron.php

You will need to change to /path/to/php to the absolute path to your PHP binary (usually something like /usr/lib/php) and change /local/path/to/cron.php to the local path to the cron.php file in your Magento root.

Testing The Magento Cron Jobs

It is difficult to test that the Magento cron jobs are now set up correctly without waiting a couple of days and seeing whether your indexes have been refreshed. Fortunately, there is a quick test you can perform to tell. Change your cron tab command to one of the following (depending on the method you chose).

MAILTO=you@yourdomain.com
*/5 * * * * wget-bad-command -q http://www.yourdomain.com/cron.php
MAILTO=you@yourdomain.com
*/5 * * * * /path/to/php -f /local/path/to/cron-bad-filename.php

The mailto commands tells the cron tab where to forward any output created by the cron script. You need to change that email address to your email address. Then notice that in both methods I have added an error. In the first method, I have changed the wget command to another command that won't exist. In the second method, I have changed the path to the cron.php to a file that doesn't exist. Now, if your cron job has been set up correctly, every five minutes you should get an email saying that there was an error with the cron script. Once you have that email you can safely revert back to the correct version and be happy knowing your cron scripts are set up! If you don't receive this email, do a Google search for Magento Cron Jobs and try to find where you have gone wrong. If that fails, send me an email and I'll see whether I can help.

Magento Cron Jobs & Custom Modules

Another great thing about Magento's cron job is that you can add your own custom modules to it!

<crontab>
    <jobs>
        <cronjob_name>
            <schedule><cron_expr>*/5 * * * *</cron_expr></schedule>
            <run><model>mymodule/my_model::functionName</model></run>
        </cronjob_name>
    </jobs>
</crontab>

Add the above code into one of your custom modules config.xml, changing mymodule for your module name, my_model for your model name and functionName for the model function you want to call.