WordPress wp-cron.php consuming too much memory and cpu
This article was posted in: Web Hosting
A victim of its own success
WordPress started out life as a simple blogging application, but has grown and developed at an almost exponential rate in the past few years. It has become perhaps the most popular CMS on the planet, driving millions of websites.
It is very extensible, allowing themes and plugins to allow just about any functionality you might want. It's still the best blogging platform, but now works incredibly well as a normal CMS, CRS, or e-commerce platform.
What does wp-cron.php do, anyway?
wp-cron.php is a script that carries out the periodic maintenance tasks that WordPress needs to do. This can include managing:
- scheduling posts to appear in the future
- emailing notifications of new comments or other events
- running plugins such as Akismet to filter comments for spam
The scale of the problem with wp-cron.php
On a small website with a few pages and a few hundred visitors a day wp-cron.php isn't a problem, but imagine a larger website with maybe a 100 visitors an hour. If each visitor read your home page, and 2 others, that would mean that wp-cron.php is being called 300 times an hour, 7200 times a day and so on. Plus, on a busy/large website wp-cron.php might take a few minutes to run.
Multiple copies of wp-cron.php can be started at once as well, because people don't wait in line to view your web pages! This means that more memory is needlessly consumed. It all snowballs into a horrible resource hog.
We've seen some busy sites running multiple instances of wp-cron.php tasks consuming over 200Mb of virtual memory each! This will quickly exhaust your cPanel accounts' allowed memory allocation - that's if it hasn't exhausted your CPU allowance already. This may cause your website to fail to operate correctly.
How to stop wp-cron.php hogging resources
The first thing we need to do is stop WordPress from running wp-cron.php willy nilly when it feels like it! Fortunately this requires no more than entering an extra line in wp-config.php, which can be found in the base directory of your WordPress installation.
Just add this line either to the top (just after the <?php tag) or bottom of the file:
This will disable wp-cron.php from being called by WordPress itself.
The next step is to setup some way of running the script manually at a regular interval. The way to do this is by creating a cron job (which can easily be done using the Cron jobs icon your cPanel account). To find out how to setup a cron job in cPanel just read or watch these articles:
- How to configure scheduled tasks using Cron in cPanel
- How to configure a cron job?
Once you have decided how often to run your job, the Command to enter will be
/usr/bin/wget -O /dev/null http://www.mydomain/wp-cron.php?doing_wp_cron
Obviously, replace "www.mydomain" above with your own website address.
How often should wp-cron.php run?
How often do you update or tend to your website?
As a rule if you only check your website once a day, then run wp-cron.php once a day. If you find that you are getting a LOT of spam comments and Akismet is not keeping on top of it, then you may want to set it to every 6 hours or so.
Even if you were constantly working on a very busy site you would probably only want things to be processed every 30 minutes at the most.