18 April 2012

Magento cron jobs not running

Sometimes you can find the cron jobs for your Magento store just aren't running, or are running intermittently even though everything has been setup correctly and there is apparently no reason for it.

Surprisingly you might find the problem is actually down to a simple caching issue with a one line code fix.  Magento caches it's cron schedule and reads this cached content on subsequent runs of cron rather than recreating the schedule each time to determine which jobs it should execute.  The problem is sometimes jobs just don't get cached despite them being configured correctly so when the time comes for them to execute nothing actually happens.

It's worth noting that the cron schedule is cached even if all caching is disabled in admin, and the only way to clear the cached schedule through admin is to Flush Cache Storage under System->Cache Management.  Unfortunately this doesn't then cause the jobs to be correctly cached the next time cron is run.

So the fix is to simply disable the caching of the cron schedule, and don't worry, benchmarks with caching disabled shows this to have no negative impact on site performance.

Copy:
app/code/core/Mage/Cron/Model/Observer.php
to:
app/code/local/Mage/Cron/Model/Observer.php
if it's not already there. Open the copied file and find the following line inside the generate() method:
Mage::app()->saveCache(time(), self::CACHE_KEY_LAST_SCHEDULE_GENERATE_AT, array('crontab'), null);
This is the line that initiates caching of the cron schedule, so just comment it out:
//Mage::app()->saveCache(time(), self::CACHE_KEY_LAST_SCHEDULE_GENERATE_AT, array('crontab'), null);
Save the changes and Flush Cache Storage to clear any currently cached cron jobs and your store should now always execute properly configured cron jobs.

21 comments:

  1. Spent hours trying to work out why a new cron job was not showing up. The "Flush Cache Storage" button sorted it. Cheers :)

    ReplyDelete
    Replies
    1. Great, glad the above sorted out your cron problem.

      Delete
  2. Yes I just noticed this when I finally decided to check it. I thought they were all running just fine. You have just given this solution to other problem. Thanks.

    ReplyDelete
  3. where do you disable cron caching?

    ReplyDelete
  4. This is just what I needed. My magento DB was becoming way too large and the cron wasn't emptying the log data. Thank you.

    ReplyDelete
  5. Just found this as i'm having problems with my product alerts not sending. I can't see which line needs taken out above, has it been removed?

    Cheers

    ReplyDelete
    Replies
    1. The line to be commented still exists in the latest version of Magento CE (1.7.0.2 at the time of writing) and is still in the location as described above.

      Delete
  6. My newsletter and Product Alert Emails are not working, this can fix the two?

    Best Regards,

    Marcelo

    ReplyDelete
    Replies
    1. If the issues you are having are caused by cron jobs not running which should be, then it will probably fix the issue yes.

      Delete
    2. Hi, in my magento store the path: "app/code/local/Mage/Cron/Model/" don't exist, I have to create?
      PS: I'm using magento v1.6.1.

      Best Regards,

      Marcelo

      Delete
    3. Hi Marcelo, yes create the path and copy the file over.

      Delete
    4. Hi Hussey, the "Start Time" in "Product Alerts Run Settings" uses the server time?

      Delete
    5. Magento will schedule cron according to whatever timezone you have your install set to, this may not be the same as server time.

      Delete
    6. Hi, unfortunately the tutorial don't worked for me, I made everything that is on the tutorial and put a product out of stock, after that I clicked in the link to receive stock alerts, I waited more then 24 hours to see if works, but I don't received any email about the product alerts, do you know what can be the problem?

      Delete
    7. Hi Marcelo,

      It sounds like something else is stopping your cron jobs running correctly - this solution is proven to work when the caching of cron schedules is the problem.

      I would be happy to look at your site and diagnose the issues you are having, if you would like me to then you can get in touch by following the link at the top to the Hussey Coding website and using the contact form there.

      Delete
    8. Hi, I tried to contact you using your contact form(Get in touch) but the submit button is not working.

      Delete
    9. Hi Marcelo,

      The submit button will only become active when you have filled out each field correctly. Fields which are not correctly filled out are highlighted.

      Delete
  7. You are my here right now. Spend hours on figuring out why my custom cron job was not fired. It was cheduled, but never got to the succesfull state. Now it does. Thank you!

    ReplyDelete
  8. Thanks for this. I spent two weekends trying to figure this one out. I thought only one item was not running properly... turns out there were a lot more. This should be a sticky one Magento's forums.

    ReplyDelete