Pages

12 July 2011

Troubleshooting Magento cron

Getting cron to work correctly in Magento seems to be the source of headaches for a lot of people, if that's you then hopefully following this through will allow you to get it working.

The first thing to do is to make sure the server is actually executing a cron job. From a terminal log on to your server as the user you want to setup the cron job under:

ssh username@server

Find out what jobs are currently scheduled in crontab:

crontab -l

If there is no output, you have no cron jobs scheduled, otherwise you may see something like the following:

*/15 * * * * /command/to/run.sh

If any scheduled cron jobs are set to run either cron.php or cron.sh in your Magento install directory, then crontab should already be configured properly. If you had no output, you need to add a job. If you are not familiar with cron syntax, you might want to have a look here, otherwise you can just go ahead and run:

crontab -e

This will launch a text editor so add your job onto a new line but don't save and quit just yet. At the top of the file add the following mailto line (if it's not already there) so that cron will email any output from the commands to the specified email address:

MAILTO=some@emailaddress.com

If everything goes to plan, you may not get any output from crontab when the job runs, so to make sure we do, edit the cron command to point at some location that doesn't exist, and change it to run every 5 minutes, so you will end up with something like this:

MAILTO=some@emailaddress.com
*/5 * * * * /some/fake/location.sh

Save the file and within 5 minutes you should receive an email from cron showing that the job has run. This proves the server is running the cron job, so if you don't get an email crontab may not be correctly configured and this should be looked at.

Generally I see people recommend kicking off Magento's cron with one of the following:

*/5 2 * * * /usr/bin/php -f /path/to/magento/install/cron.php

*/5 2 * * * /path/to/magento/install/cron.sh

Both of these can potentially be problematic however - cron.sh may need editing depending on your servers default shell, and using PHP CLI to call cron.php can fail to correctly schedule jobs if the user executing the cron job does not have sufficient privileges. Another method and the one that I prefer is to use wget:

*/5 2 * * * wget -q -O /dev/null http://server/cron.php

Not only does this bypass potential shell problems with cron.sh, it also ensures the script is executed as the correct user.

Add the wget cron job (but tailored to your needs) into your crontab file which should still be open, save and close.

Assuming the cron job is running correctly and you received the email to prove that then you can start looking at Magento itself. I'm not going to go into how you configure a module to use cron, or where to find cron configurations in an already existing module as if you have come across this page you have probably already got that information. Instead I will cover how to troubleshoot Magento cron jobs that do not appear to be running.

The key to finding out whether or not Magento has scheduled cron jobs to run is the cron_schedule table. Any jobs which have been queued up by cron will be shown in here with time and current status details.

From your terminal window (hopefully you still have it open) log in to MySQL and switch to the Magento database:

mysql -u database_user -p
mysql> use your_database

If you are unsure of your database credentials, they will be stored in app/etc/local.xml under your Magento install.

You can then see what content is in the cron_schedule table:

mysql> SELECT * FROM cron_schedule;

In the table that's printed out, you will a job_code column which should be fairly self explanatory as to what the job relates to, and a few columns detailing when the job was added and run.

You will also see different text under the status column such as pending, running and success, the table will also be purged intermittently. If everything is working as it should be then you hopefully will only see a status of success, apart from if a job is actually running at the time in which case the status may be different but should change to success the next time cron is run. However if the status for a job is pending and it is not a recent one (perhaps a couple of days old or more), this could indicate a problem with that cron job completing as the status here will only be set to success when the cron job runs through without issue. It's also worth noting that a problematic cron job can stop other jobs from running.

Hopefully this has helped you to get your cron jobs running correctly, or if not at least track down where the problem lies.

If you have tracked down a problematic cron job, you can clear the job from the cron schedule table by running:

mysql> DELETE FROM cron_schedule WHERE status='pending';

Then, for further testing you might want to temporarily change the cron scheduling for the module in question to just ahead of the current time and run your crontab job from the command line to further investigate the problem.

wget -q -O /dev/null http://server/cron.php

Obviously remember to change back the cron scheduling for the module once finished.