Getting cron to work can be a pain so I thought I’d use this thread to note down some tips I’ve found in my struggles through it.
Log when cron runs
It can be hard to tell if cron really is running when you think it should be, you can check the database but that can be confusing. Fortunately it’s easy to add a magento log event to each cron call, just edit cron.php in your store root as follows then turn on logging in System > Configuration > Developer > Log Settings
Mage::app('admin')->setUseSessionInUrl(false);
try { Mage::getConfig()->init()->loadEventObservers('crontab'); Mage::app()->addEventArea('crontab'); Mage::dispatchEvent('default'); Mage::log("Cron run"); //Add this line and you will get a log entry } catch (Exception $e) { Mage::printException($e); }
Now whenever cron is triggered you will get an entry in /var/log/system.log like this:
2010-01-20T01:15:05+00:00 DEBUG (7): Cron run
Trigger Cron manually
To test the logging script or anything else cron related you can trigger it manually by pointing your browser to
http://yourstoreaddress/cron.php
Check the log file after hitting that address and you should see a new cron entry.
Understand the times that Magento and your server use
Magento internally sets all cron dates to occur in GMT, it actually overrides the server php time zone settings so that all log events and all cron time schedules are triggered in the GMT timezone - you can see this in Mage.php on line 527. All php time functions called within Magento will be in GMT as a result (like the timestamp in the log call above).
Magento then adds back your timezone offset based on your locale settings when it shows you times in the admin panel.
It does this so you can run multiple stores in different timezones based on your locale choice and ideally you would never know nor care about this - but it makes debugging cron events really confusing.
Worse, in 1.3.2.4 there are some bugs, if you enter in a trigger time for a product alert the time doesn’t get converted back to GMT from your timezone when its saved to the database, yet any reports on when product alerts were run is converted to your timezone on display. so its a nightmare to debug. In effect the alert will run as if you had entered it in GMT but will report as if it occurred in your timezone.
Check how cron is configured with your hosting company
Depending on your host’s setup crontab events may or may not execute as your normal account user, so some of the paths to trigger cron.php floating around the forum/net may not apply to your case (examples using wget or curl should be fine). If your log file shows that cron.php is not getting run, yet you can trigger it by hitting the cron page manually then contact your host and get them put together the appropriate cron entry for your system.
Set up your Cron log so that you aren’t limited to a ridiculously short time span for monitoring finished jobs. Otherwise, all you see is that darn newsletter_send_all.
Go into your Advanced System Config and set Cron Success History Lifetime and Failure Lifetime both to 1440 so you are monitoring a 24 hour span of time.
You will now be able to see index operations, etc in the time stream. There will be about 300 jobs listed in your Jobs Successful section over the 24 hour timespan.