[ad name=”postsqrRight”]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. 🙂
[ad name=”posts”]
What if i chmod the wp-cron.php to 000? What negativ effect will this have?
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).
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
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! 🙂
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!
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
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! 🙂
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!
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.
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 😉
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.
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 😉
There’s nothing wrong with HostGator! In fact, I use them over Linode now for my bigger clients.
Any host who offers unlimited diskspace and bandwidth is horrible, I am not a fan of lying and false advertising. 🙂
There’s nothing wrong with HostGator! In fact, I use them over Linode now for my bigger clients.
Any host who offers unlimited diskspace and bandwidth is horrible, I am not a fan of lying and false advertising. 🙂
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
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
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.
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.
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.
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. 🙂
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.
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.
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.
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.
Hi Dan,
Thanks for this information. Just to clarify. In this command:
php -q /home/cPanel_User/public_html/wp-cron.php
cPanel_User is replaced by the username of the database, correct?
Just checking. Thanks.
JD.
It’s the cPanel username, not the database username. Although in most cPanel installs the cpanel username prefixes the db name. Eg. if it’s called dan_wordpress the cPanel username is probably “dan”
OK, so I never really paid attention to Updates or Update scheduling. why do I need to add code to the tab to poll every 60 minutes when right below Updates, there is a field that allows me to select to poll every so many minutes? I think my blogs get shut down every time the spiders come to crawl because they trigger updates, maybe??? What am I missing here? 🙂
You say to add this line to the wp-config file but other articles say to add it to the top line of wp-cron itself? Does it make a difference