Home » Magento tutorials » How to set up Magento CRON Job

How to set up Magento CRON Job

What is Cron?

Cron jobs are automated events that are triggered by the server. A lot of people think that the Magento cron job runs automatically – but this is not the case.

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?

Cron Job helps you perform some specific actions automatically. So depending on your needs, you can decide whether to set up Cron Job or not. For example, without a Cronjob, you will have to manually refresh catalog price rule or catalog indexes several times a day when you update product price rule. If you’re running a big store this will take a lot of time, which is not worth.

Setting Up Magento’s Cron Job

After you finish setting up your Magento website and set it live, you MUST activate the Magento Cron – otherwise the following functionality will not work:

  • Newsletter
  • Sitemap Generation
  • Other extensions that rely on cron settings

First step to set up your cron: you need to tell your server to execute Magento’s cron.php  file regularly – about every 10 minutes or 20 minutes – you can change this depending on how long it takes your cron to run (normally only a couple of seconds but if you have extensions that do a lot of processing you may want to extend the time between runs – the last thing you want is for a cron to overlap!).

Now we will go through how to set up Cronjob within a Plesk control panel.

First of all navigate to your domain and then click on Crontab (sometimes called ‘Scheduled Tasks’). Within Crontab will be a list of users for the domain (normally your FTP users) – click on a user and then click on Schedule a Task for <user> (or sometimes just ‘Schedule New Task’).

This next page’s where we are going to set the time and dates for our cron to run – and also the command that will be ran.

For a Magento cron, we want it to run every 10 minutes or so. To do this we can enter the following details into the fields:

  • Switched on = checked
  • Minute = */10 (star means ‘every’ – so here we’ve said every 10 minutes)
  • Hour = *
  • Day of the Month = *
  • Month = *
  • Day of the Week = *
  • Command = /usr/bin/php -f /var/www/vhosts/mydomain.com/httpdocs/cron.php

cron-task2[1]

So, above we have set our cron task to run every 10 minutes, every hour, every day, every month and to run the command:

Or alternatively (and probably better in my opinion) run it via CURL:

Please note that you must edit this command to match your own server setup – it’s the path from the root of the server to the cron.php residing in your Magento installation (don’t copy/paste!).

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).

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!

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.

That is pretty much all you need to do to get your cron up and running. I hope you enjoyed this quick tutorial!

About Brian Wilson

avatar
Brian is a freelance Magento developer with 5 years of experience with customizing Magento extensions, magento themes. He's interested in sharing Magento Tutorials to the community

One comment

  1. avatar

    On some servers with limited cron feature, or the webmasters want to manage cron jobs with easier to use interface, WebCron EasyCron.com could be an option.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Scroll To Top