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 🙂
Great!
Just what I needed. ;-D
Thanks lol u saved me ..i have made changes ..lets see what happens
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 🙂
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.
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
You should plan to run the cron using cronjobs. Did you try that?
Thanx, but doing this won’t stop the scheduled posts stop?
It would. That is why you would need to run the wp-cron.php using custom cronjobs from cpanel on a regular basis.
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.
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
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!!! 🙂
Sir I have Done what u said above but it still showing 100% CPU Usage
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
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
I was trying to configure cron job in my WordPress website and found this post helpful in fixing that issue, I have fixed on the platform here.