Magento Cron Jobs

piaoling  2011-06-23 18:53:03

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:

?
1
crontab -e

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

?
1
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

?
1
*/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

?
1
*/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).

?
1
2
MAILTO=you@yourdomain.com
*/5 * * * * wget-bad-command -q http://www.yourdomain.com/cron.php
?
1
2
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!

?
1
2
3
4
5
6
7
8
<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.

This post was posted in General

4 Responses to Magento Cron Jobs

  • Hi,
    I have a question:
    If the cron.php runs every 15 minutes and the crontab runs every 3 hour, how often crontab runs?
    Thanks in advance..

    Posted on July 7, 2010 at 9:42 am

  • Julz says:

    For Emilio...

    "crontab" is two things (maybe more but for this reference just two), it's a file called crontab that contains a list of commands with intervals for executing those commands seperated by newlines. It's also the name for the program that is used to create, modify and delete the "crontab" files. This program is typically used from the shell by a user to act on their own "crontab" file.

    Cron is the process/job that runs intermittently (I think it's every minute well it should be because you can specifiy a job to be run by cron down to a minute interval). It processes system-wide crontabs and user crontabs. See [http://www.unixgeeks.org/security/newbie/unix/cron-1.html] for more info.

    Hope that helps somewhat.

    Oh and to answer your question, sort of, the crontab is most likely executed every minute and therefore if the "cron.php" task in the "crontab" is setup with an interval of every 15 minutes then the task runs pretty much every 15 minutes :)

    Posted on August 5, 2010 at 5:56 am

  • I'm running my cron job every minute, but setting my magento cron_expr to run every 5 minutes. For some reason my custom module is being executed every minute.

    I cannot have every magento cron job running every minute!

    Is anyone else esperiencing this problem or am I missing soemthing somewhere?

类别 :  magento(258)  |  浏览(3796)  |  评论(0)
发表评论(评论将通过邮件发给作者):

Email: