Magento Cron
What is Cron?
To quote Wikipedia, 'cron is a time-based job scheduler in Unix-like operating systems'. Its functions are similar to Windows Task Scheduler that allows you to specify actions to be run 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 (for example, Magento SMTP extension), which we will cover shortly.
You may also need cron to configure automatic process for Magento Export Orders module. See the details on the product page.
Does Magento Need Cron?
All Magento eCommerce installations should have a cron job setup. The major benefit of 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 have to perform these tasks manually. For example, without a Magento cron job set-up you should 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 in case you forget to do this you could lose your money!
Magento's Cron Job Setting Up
Firstly you need to open your cron tab, which is where you enter your cron commands
The Cron Tab Opening
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 change USERNAME with the username you want to run. Now 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 the cron.php file in your Magento root.
The Magento Cron Jobs Testing
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. Change your cron tab command to one of the following (depending on the method you chose).
1
2
|
|* /5 * * * * wget-bad- command -q http: //www .yourdomain.com /cron .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 we have added an error. In the first method we changed the wget command to another command that won't exist. In the second method we 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.
Login and Registration Form