How to stop wp-cron.php from firing!

Recently, I have written an article about reducing the cpu usage for a wordpress blog. My post contains some information about the wp-cron.php, but it doesn’t explain how you can stop wp-cron.php from taking high CPU. Couple of our twitter followers and facebook clients were asking for a post describing how they can stop wp-cron.php from taking high CPU or firing up. Here are some small tricks to reduce the CPU usage from wp-cron.php.

First of all, if you have root access to your server, you can eventually block wp-cron.php using mod_security, this would prevent wp-cron.php usage throughout the web server. But you can still call it using the cronjobs. How to setup wp-cron.php manual cronjobs, can be found in my blog post “Reducing CPU Usage for WordPress blog” post.

If you are on a shared hosting or want to permanently stop this culprit, you would need to stop spawning this php file. wp-cron.php calls cron.php file which is located under your wordpress root/wp-includes/ folder. Open the file in your file manager or using FTP browser and find the line stating:

spawn_cron( $local_time );

Now comment this line and you should stop wp-cron.php spawning everytime an user enters your site. You can comment it with two slashes as following:

// spawn_cron( $local_time );

You have to keep in mind, this would stop all sort of scheduled event as well.

I could discover another alternative that would stop wp-cron.php from running using http request, but would work fine using cronjobs. Open cron.php and find the following line:

if ( strpos($_SERVER[‘REQUEST_URI’], ‘/wp-cron.php’) !== false || ( defined(‘DISABLE_WP_CRON’) && DISABLE_WP_CRON ) )

Now, replace this line of code with the following:

if ( strpos($_SERVER[‘REQUEST_URI’], ‘/wp-cron.php’) === false || ( defined(‘DISABLE_WP_CRON’) && DISABLE_WP_CRON ) )

Make sure the immediate next line “return” must remain in the immediate line.

You can also put the following in your wp-config.php file to set DISABLE_WP_CRON global variable to TRUE:

define('DISABLE_WP_CRON', true);

At last, you should make sure wp-cron.php runs using cronjobs for your scheduled events, but if you don’t have scheduled events, then you better stay away from adding it in cronjobs as well. But if you do, then have a look at my previous post for setting up the manual cronjobs of wp-cron.php using cpanel.

Any of the above solution should prevent your wp-cron.php from firing and taking high CPU. Happy blogging 🙂

14 thoughts on “How to stop wp-cron.php from firing!”

  1. was wondering if I comment out spawn_cron( $local_time ); means that the scheduled posts wont’t be published anymore?

    “this would stop all sort of scheduled event as well.” that is a sort of scheduled event, and if stops all, than it’s not good for me 🙂

    1. That would. You would need to disable the wp-cron.php and run it using cron. That would let you run the cron at the specific time instead of running each time a visitor visits.

  2. Hello!

    Great article here. Im facing this problem with my hosting provider. The admin told me this technique will prevent my site using a lot of server resourcers so im reading all king of posts regarding to this matter.

    What concerns me the most is what will happen to RSS Feeds and work of my other 3 editors. What exactly this change will do in my blog? Can u be a little more especific?

    Thanx a lot

  3. Thank you for this excellent article. Until my site started missing scheduled posts, I had no idea this was an issue. Calling cron every time a user shows up at the site seems pretty crazy to me, and I’m grateful for the simple solution to this problem.

  4. I got my blog suspended for this wp-cron thing. Contacted servers and they said it was the reason I was suspended. They did something to prevent it from working and then it was back

  5. Editing core WordPress files is a dangerous solution, as it will simply be overwritten the next time the site is updated – and then the site behaviour would change in strange ways.

    The safe way to do this is to define the DISABLE_WP_CRON entry in wp-config.php and arrange to call your cron file from your host’s crontab file.

    If your blog was suspended due to cron usage, it’s not cron itself that is the problem, it’s the foolish plugin authors that are running cron jobs every minute without thinking through the consequences. You should install one of the many plugins that let you see scheduled cron jobs (eg: cron-view), and disable those plugins that run every minute.

    Then go to the WordPress.org plugin page, click on support, and submit a post there asking the plugin author to fix his/her code and that you got suspended due to their cron usage. 🙂

    But never, ever, edit WordPress core – it’s dangerous to your health, happiness and general sense of well being!!! 🙂

  6. Are you saying I should comment out spawn_cron like below;

    // spawn_cron( $local_time );

    And, also stop wp-cron.php from running using http request, by doing this too? As below;

    if ( strpos($_SERVER[‘REQUEST_URI’], ‘/wp-cron.php’) !== false || ( defined(‘DISABLE_WP_CRON’) && DISABLE_WP_CRON ) )

    Now, replace this line of code with the following:

    if ( strpos($_SERVER[‘REQUEST_URI’], ‘/wp-cron.php’) === false || ( defined(‘DISABLE_WP_CRON’) && DISABLE_WP_CRON ) )

    Should I do one or the other or both. I am running WP 3.5 Multisite Install. I want to call wp-cron.php from the crontab twice a day. I do not want it to run anymore than twice a day under any circumstances.

    I am also calling Feedwordpress twice a day from the crontab but I am assuming this will not interfere.

    By doing both am I somehow preventing WP from doing anything important? Should I only do one? Currently, I have done both procedures as suggested above.

    Any reply is greatly appreciated.

    Thanks,

    Carlos

  7. This is just what I needed. Installed the plugin which went very smoothly and the info it generated allowed me to delee some crons and two plugins.
    Just hope this will satisfy Hostgator who have been next to no support, must find another host

Leave a Reply

Your email address will not be published. Required fields are marked *