Issues with the cron-like function in WordPress

WordPressI recently came across this issue in a WordPress installation. My friend’s page (www.sinuniforme.com — a very good Venezuelan based sports page BTW) had become down because of high resource usage in the virtual private server he is using to host it.

These are some of the Apache logs the service provider sent him as proof of the irregular high consumption (and justification for shooting down the webserver within the VPS while the problem wasn’t addressed):

sinunifo 14285 0.8 1.3 107952 35684 ? D 10:35 0:00 /usr/bin/php /home/sinunifo/public_html/wp-cron.php
nobody 14288 0.0 0.1 71004 3236 ? S 10:35 0:00 /usr/local/apache/bin/httpd -k start -DSSL
sinunifo 14289 0.8 1.3 107956 35688 ? D 10:35 0:00 /usr/bin/php /home/sinunifo/public_html/wp-cron.php
nobody 14290 0.0 0.1 71004 3236 ? S 10:35 0:00 /usr/local/apache/bin/httpd -k start -DSSL
sinunifo 14291 0.8 1.3 107960 35688 ? D 10:35 0:00 /usr/bin/php /home/sinunifo/public_html/wp-cron.php
nobody 14295 0.0 0.1 71004 3236 ? S 10:35 0:00 /usr/local/apache/bin/httpd -k start -DSSL
nobody 14296 0.0 0.1 71004 3236 ? S 10:35 0:00 /usr/local/apache/bin/httpd -k start -DSSL
sinunifo 14297 1.0 1.3 107956 35688 ? D 10:35 0:00 /usr/bin/php /home/sinunifo/public_html/wp-cron.php
nobody 14298 0.0 0.1 71004 3236 ? S 10:35 0:00 /usr/local/apache/bin/httpd -k start -DSSL
sinunifo 14299 0.9 1.3 107952 35684 ? D 10:35 0:00 /usr/bin/php /home/sinunifo/public_html/wp-cron.php
nobody 14300 0.0 0.1 71004 3240 ? S 10:35 0:00 /usr/local/apache/bin/httpd -k start -DSSL
sinunifo 14301 0.8 1.3 107956 35688 ? D 10:35 0:00 /usr/bin/php /home/sinunifo/public_html/wp-cron.php
sinunifo 14302 0.9 1.3 107956 35688 ? D 10:35 0:00 /usr/bin/php /home/sinunifo/public_html/wp-cron.php
nobody 14307 0.0 0.1 71004 3236 ? S 10:35 0:00 /usr/local/apache/bin/httpd -k start -DSSL
sinunifo 14308 0.8 1.3 107960 35688 ? D 10:35 0:00 /usr/bin/php /home/sinunifo/public_html/wp-cron.php

And that’s just a small abstract. If you take a close look you’d see that there are a lot of processes handling calls to a php file called wp-cron.php.

Doing some research I found out that wp-cron is wordpress’ implementation of a cron-like function, used for things like automatic publishing of posts in the future, automatic checking of updates (of plugins, themes and core code) and the sort. If I understood correctly, every time a post is served the routine is called. Once called it checks if there are scheduled tasks that need to be run. It’s supposed to be a very light-weight procedure. But it actually was taking down my friends page. I’ve read of similar stories on the web (google for wp-cron.php for examples). It looks like it happens to very high-traffic pages.

The solution is simple. Get rid of the wordpress cron and use the server’s cron (of course, you need root access to the server, which is very likely the case if you have a high-traffice site).

There are three steps involved:

1.- Disable wp-cron. Go to your wp-config.php file and add the following line on the define section:

define('DISABLE_WP_CRON', true);

2.- Modify wp-cron.php so its not run when a post is served.

Change the lines:

if ( !empty($_POST) || defined('DOING_AJAX') || defined('DOING_CRON') )
 die();

To:

if ( defined('DOING_AJAX') || defined('DOING_CRON') )
 die();

3.- Schedule a cron job from the operating system to call the wp-cron.php page on a regular bases (it could be every 15 min, 1hour, 1 day… I depends on the use you give it — I set it to run once everyday)

wget http://www.server.com/wp-cron.php?doing_wp_cron

With that you should be good to go. For me it worked, my friend was able to start the webserver and received no further complaints from he’s hosting service.

I got most of the info for this post from these two posts:

Posted in Web, WordPress. Bookmark the permalink. RSS feed for this post. Both comments and trackbacks are currently closed.

Swedish Greys - a WordPress theme from Nordic Themepark.