Setting up the Magento cronjob

From XTENTO Support Wiki
Jump to: navigation, search

This is a guide for Magento 1. For Magento 2 extensions and guides, please refer to Magento 2 Guides.

Contents

What is cron or a cronjob and why do we need it?

According to Wikipedia, the cron is
a time-based job scheduler in Unix-like computer operating systems. Cron enables users to schedule jobs (commands or shell scripts) to run periodically at certain times or dates. It is commonly used to automate system maintenance or administration

Why do we need a cronjob in Magento? Because some tasks need to be automated - you don't really want to refresh the catalog index in Magento every couple of minutes, do you?

Magento uses the cron.php file sitting inside the root directory of Magento to manage and dispatch cronjobs. The cron.php script basically executes the internal Magento cronjob manager, managing which and when cronjobs are executed.

Some of our extensions use cronjobs to run certain actions automatically at specified intervals, for example the Tracking Number Import Module can be configured to import shipments/tracking numbers every couple of minutes or every hour.

Setting up the Magento cronjob

Please set up the Magento as recommended by Magento using cron.sh and make sure cron.sh is called every 5 minutes.

It is VERY important your PHP configuration is the same for php-binary-based executions. If it's not the same php.ini file/configuration as for webserver/Apache executions, some things may not work.

Setting up the Magento cronjob [Old method, recommended until Oct 2015]

This method is not recommended anymore as with Magento 1.9.2.2 cron.php cannot be called via HTTP anymore. Use cron.sh and not wget/cron.php.

In order to set the Magento cronjob up you will need either access to the server control panel or will need shell (SSH) access for the server where Magento is installed on. You can also try contacting your hoster or server administrator and ask them to set up the cronjob for you.

Setting up the Magento cronjob using shell access

It is strongly recommended not to run the cronjob under the root/superuser account. If you are logged in as root, please switch (su) to a non-privileged account.

crontab -l

crontab -e

*/5 * * * * wget -O /dev/null -q http://www.YOURDOMAIN.com/PATH_TO_MAGENTO/cron.php > /dev/null

It is very important that the cron.php file of Magento gets executed every five minutes. Do not change this to a longer interval.
Please try to call the URL you've entered into the cron tab using your browser. A blank page should be returned. If the page can't be found (404), that means the URL to the cronjob manager of Magento (cron.php) is wrong. This means the cron job won't execute and hasn't been set up properly. It is really important you enter the correct URL - it is always the URL to the cron.php file sitting inside the root directory of your Magento installation.

crontab: installing new crontab

Setting up the Magento cronjob using control panel access (cPanel, Confixx, Plesk, ...)

If you've got access to a server control panel, setting up the cronjob is as easy as going to the Cronjob Manager inside the control panel. Make sure no other cronjob has been set up calling the cron.php file of Magento, and if that's the case, remove it before adding this cronjob. Add a cronjob with the following parameters:

This means the cronjob will be run every 5 minutes. It is very important that the cron.php file of Magento gets executed every five minutes. Do not change this to a longer interval.
Replace YOURDOMAIN.com and PATH_TO_MAGENTO with the URL of the root directory of your Magento installation.
Please try to call the URL you've entered using your browser. A blank page should be returned. If the page can't be found (404), that means the URL to the cronjob manager of Magento (cron.php) is wrong. This means the cron job won't execute and hasn't been set up properly. It is really important you enter the correct URL - it is always the URL to the cron.php file sitting inside the root directory of your Magento installation.

Why we suggest calling the cronjob via wget/curl

In our opinion, there is no advantage calling cron.sh/cron.php from the command line compared to calling the cron.php file via wget/curl. Why?

When you call PHP from the command line using the PHP binary, be it "./cron.sh" which ultimately calls "php cron.php" or directly call "php cron.php", a different PHP ini file is/can be used compared to when you're executing using a HTTP request to the webserver by calling "http://www.YOURDOMAIN.com/cron.php". By calling the URL using wget, the same PHP ini file and settings are used which are confirmed to work as you can access Magento in your browser and it works fine. But do you know that for sure using the different php.ini file used for command line (cli) calls? Also, often cron.sh gets called from the wrong directory, and also often from a wrong user which does not have read or write permissions to certain directories in the Magento directory or the Magento directory at all, which can lead to issues as well.

Calling cron.php via wget is just the safest and easiest solution to avoid problems.

Troubleshooting

My cronjob doesn't work. What can I do?

Please double check that you set up the cronjob as described above. It is very important the cronjob gets executed using wget and exactly as stated above.

I am using Magento 1.8/1.9 and I am 100% sure cron.php is set up correctly

You are affected by a Magento bug then. Please open the file cron.php (back it up before editing) in the root directory of Magento and look for (around line 47): $isShellDisabled = (stripos(PHP_OS, 'win') === false) ? $isShellDisabled : true; After this line, add: $isShellDisabled = true;

Save the file. After a couple minutes the cron should start working.

Renaming cron.php to cron2.php to avoid the cronjob getting called twice

Important: Please only follow these steps when asked to do so by a XTENTO support rep.

If two or more servers (or crontabs on several servers) hit the Magento cronjob at the same time, the Magento cron can get executed at exactly the same time twice, causing *any* cronjob of Magento to execute twice (or 3x, 4x, 5x, ... at the same time). To fix this, do the following:

This makes sure that only ONE cron is being executed.

Cron error: Too late for the schedule

This error is not caused by our modules. The internal Magento cron scheduler just says that the cronjob should have been executed (it was scheduled to run), but Magento couldn't execute it when it was supposed to execute it, because some other task was running too long, so it was "too late for the schedule". So the problem is, there is some cronjob in your system that runs VERY long, and thus our cronjob doesn't get to execute - because it would be too late, and not "on the schedule". We can recommend the "Aoe Scheduler" extension (free) using which you can see a timeline of cronjobs, to identify long running tasks. After installing, go to "System > Scheduler > Timeline View" to see this and identify the cronjob(s) which causes this problem.

wget: command not found

You can also use "curl" instead of "wget" if wget is not installed on your system:

curl -s -o /dev/null http://www.YOURDOMAIN.com/PATH_TO_MAGENTO/cron.php > /dev/null

Personal tools
Namespaces
Variants
Actions
General Information
Magento 1 Guides
Magento 1 Extensions
Magento 2 Guides
Magento 2 Extensions
Magento Integration Suite
Product Feed Guides
Toolbox