How to disable WordPress wp-cron

Posted by Dan on October 3 in General stuff, Scripts |

Recently I have been noticing that this blog has been eating its way through the servers ram like no bodies business, easily using more than 150mb before its automatically killed.

By default the wp-cron.php is run every single time a page is loaded, this includes admin pages. This can lead if you have a busy blog to a lot of cron runs, which simply aren’t needed.

What does the wp-cron do? You might ask, and even if you don’t I will tell you anyway. The wp-cron will do things like scheduled posts and can also include things that plugins want running.

So how we go about this is simple, we will disable the wp-cron from running on every page load, and then add a proper cron job.

First lets disable it:

Simply open wp-config.php and add the follow line.

define(‘DISABLE_WP_CRON’, true);

Note: You need to make sure you use an apostrophe instead of single quotes in the code here, otherwise it will not work!

Second, lets add the cron:

It is probably a good idea to run the cron job every couple of hours, for this we will add something like this.

0      */2      *      *      *

For the command you simply need to call the wp-cron either using wget OR php -q

php -q /home/cPanel_User/public_html/wp-cron.php

OR

wget http://YourDomain.com/wp-cron.php

Personally I’d prefer the php -q option as some hosts can have trouble resolving domains on the same server.

If anyone has any questions or comments please leave a comment below and I’ll try to help. :)


Related Posts:

Tags: , , ,

  • Roms

    What if i chmod the wp-cron.php to 000? What negativ effect will this have?

  • http://dansgalaxy.co.uk Dan

    Hi, thanks for the comment!

    If you have it set to 000 I believe that means it is only accessible to root, and the owner (after changing permissions) this is likely to cause permissions problems when anything other than root (and perhaps the owner, although I doubt even that) tries to access – read/write to it.

    I’d suggest something like 744 (read by all, write and exec by owner).

  • Scott

    Thanks for this Dan. Quick question, if we disable wp cron do we HAVE to set up a regular cron? I don’t understand enough about crons to know. What would happen if we disabled wp cron but didn’t set up a cron on our server?

    Thanks

    • http://dansgalaxy.co.uk Dansgalaxy

      Hi Scott,

      The WP Cron job runs numerous operations which “need” to be run regularly, this includes scheduled posts, now as we have disabled the cron from running on page load this means scheduled posts won’t be published unless the cron is run an alternative way, as well as this there are other functions which the wp-cron performs and (i think) plug-ins may also run things via cron which they need.

      Easy answer, things might not work as expected or update as expected… So it’s a very good idea to set-up a cron job on the server if you are disabling the on-page-load option for WordPress.

      Hopefully this has answered your questions, Thanks for commenting! :)

      • Scott

        Hi Dan

        Many thanks! Yep, I think I get it now lol. Guess I’ll have to give it a try as I wouldn’t want any of my blogs not functioning properly.

        I’ll come back to your article when I’m ready to disable wp cron. Thanks again!

  • Scott

    Thanks for this Dan. Quick question, if we disable wp cron do we HAVE to set up a regular cron? I don’t understand enough about crons to know. What would happen if we disabled wp cron but didn’t set up a cron on our server?

    Thanks

    • http://dansgalaxy.co.uk Dansgalaxy

      Hi Scott,

      The WP Cron job runs numerous operations which “need” to be run regularly, this includes scheduled posts, now as we have disabled the cron from running on page load this means scheduled posts won’t be published unless the cron is run an alternative way, as well as this there are other functions which the wp-cron performs and (i think) plug-ins may also run things via cron which they need.

      Easy answer, things might not work as expected or update as expected… So it’s a very good idea to set-up a cron job on the server if you are disabling the on-page-load option for WordPress.

      Hopefully this has answered your questions, Thanks for commenting! :)

      • Scott

        Hi Dan

        Many thanks! Yep, I think I get it now lol. Guess I’ll have to give it a try as I wouldn’t want any of my blogs not functioning properly.

        I’ll come back to your article when I’m ready to disable wp cron. Thanks again!

  • Paul Kaser

    Thanks, Dan. Our Hostgator shared hosting account kept getting shut down, and I’m pretty sure wp-cron was the issue. I’ve used your tips here to fix things up proper.

    • http://dansgalaxy.co.uk Dansgalaxy

      Good to hear! – other than you are using HostGator of course! Ew! (Not a fan of “unlimited” hosts)

      Don’t forget to have a look around some other posts ;-)

  • Paul Kaser

    Thanks, Dan. Our Hostgator shared hosting account kept getting shut down, and I’m pretty sure wp-cron was the issue. I’ve used your tips here to fix things up proper.

    • http://dansgalaxy.co.uk Dansgalaxy

      Good to hear! – other than you are using HostGator of course! Ew! (Not a fan of “unlimited” hosts)

      Don’t forget to have a look around some other posts ;-)

  • http://twitter.com/ajlanghorn_msp Andrew

    There’s nothing wrong with HostGator! In fact, I use them over Linode now for my bigger clients.

    • http://dansgalaxy.co.uk Dansgalaxy

      Any host who offers unlimited diskspace and bandwidth is horrible, I am not a fan of lying and false advertising. :)

  • http://twitter.com/ajlanghorn_msp Andrew

    There’s nothing wrong with HostGator! In fact, I use them over Linode now for my bigger clients.

    • http://dansgalaxy.co.uk Dansgalaxy

      Any host who offers unlimited diskspace and bandwidth is horrible, I am not a fan of lying and false advertising. :)

  • Pingback: wp-cron and MultiSite = Headache | Dan's Tech Blog

  • http://buildyourblog.net/ Graham

    Thanks for the article Dan.

    One minor quibble: in the code to add to wp-config.php, you say:
    define(‘DISABLE_WP_CRON’, true);

    Note the quotes should actually be like this: ‘ . I copied and pasted from your article into my wp-config.php, and with the wrong quotes it doesn’t work.

    Thanks, Graham

    • http://dansgalaxy.co.uk Dansgalaxy

      Hi Graham, 

      Glad you like the post :) It should be using ‘ quotes, however i think it might be a quirk of WordPress that it changes them as this is something I have run into myself when copying and pasting similar code from blogs! – I’ve added a note to the post to hopefully save confusion in future. :) Dan

  • Jasontoews3434

    Making the change in wp-config.php seems pretty straightforward, but I’m not clear how/where I add the new “every two hours” cron job. Can you give some details on that, or post a relevant link?

    Apologies; this is something new to me.

    • http://dansgalaxy.co.uk Dansgalaxy

      This would be dependant on your webhosting provider, and the control panel they use.

      If they use cPanel it should be fairly straight forward assuming you have permissions to set cronjobs there will be an icon within cPanel called Cron Jobs or similar which you can manage them there. 

      Hope this helps! Thanks for reading.

  • Dean

    Hi Dan…been having issues with HG closing one of my reseller accounts down because of usage issues and going to try your tip with the cron. They have disabled my wp-cron on each site on the account which saves me adding the disabled code. My question…I have about 20 sites on this account…do I use the same settings you’ve listed for each site when setting up the manual cron job in cpanel?

    Hope that makes sense? Thanks.

    Dean.

    • http://dansgalaxy.co.uk Dansgalaxy

      Assuming each site is a separate wordpress install then yes you’ll need to run the wp-cron.php for each install as (I assume) each will be running different code and connecting to different databases etc to perform their actions. :-)

      • Dean

         Hi Dan…as I mentioned in the first comment, HG have disabled my wp-cron from their end which I suppose means I don’t have to add this disable code… define(‘DISABLE_WP_CRON’, true);

        I have set up the cron through my cpanel on one site as you suggest and I get an email every couple of hours when it runs. This is the message I get:

        Cron php -q /home/cPanel_User/public_html/wp-cron.php…No input file specified.

        Does this sound right? Is this because the wp-cron has been disabled? Sorry, just wondering if it looks right. Appreciate your help. Thanks.

        Dean.

        • http://dansgalaxy.co.uk Dansgalaxy

          It depends *how* HostGator in their wisdom has disabled wp-cron.php, it might be they have disallowed the wp-cron.php file from executing. Check your wp-config.php to see if the disable_wp_cron line has been added, if it’s not there chances are they disabled it by changing the file permissions on the wp-cron.php file or simply deleting the file.

          • Dean

             Ok..thanks. No, they have changed the file permissions. Do I still need to add the line of code?
            Appreciate the time you have taken to answer my questions.

          • http://dansgalaxy.co.uk Dansgalaxy

            I’d add it just to be sure, but you should ask how they disabled the cron as need to ensure you won’t run into issues due to whatever they have imposed. 

Copyright © 2007-2012 Dan's Tech Blog All rights reserved.
Desk Mess Mirrored version 1.9 theme from BuyNowShop.com.