Web Apps / Wordpress

WordPress Cron Optimization

by , , revisited on

We have by far the largest RPM repository with NGINX module packages and VMODs for Varnish. If you want to install NGINX, Varnish, and lots of useful performance/security software with smooth yum upgrades for production use, this is the repository for you.
Active subscription is required.

WordPress has not only became a popular blogging platform in last years. It is now used for building complex enterprise websites. However, it has some weak performance spots by default. One of those spots is WordPress cron.

The default crontab runs using internal WordPress schedule. If there are no hits to your website, then crontab simply doesn’t run. If there is a hit and it happens that the user visited the site when cron has to run, their visit will be slow.

Fix WordPress Cron Performance

To make sure things run smoothly, we have to make WordPress cron tasks are launched by real Linux cron service.

We have to edit crontab as site’s user, not as root: crontab -e will launch crontab editing screen.

* * * * * /usr/bin/php /var/www/html/wp-cron.php

This crontab entry above will make sure that WordPress cron is run every minute.

If you want to have it run every other minute instead, use:

*/2 * * * * /usr/bin/php /var/www/html/wp-cron.php

Now all we have to do, is tell WordPress that it does not need to run cron tasks, because we have it handled by Linux system. Edit wp-config.php and put a line after opening php tag, like this:

define('DISABLE_WP_CRON', true);

Tap yourself on the back for making your website’s users happy. Their browsing experience is not affected by WordPress cron anymore. WordPress internal tasks will always run on time.

Filter some errors

If you were to disable error_reporting function like we recommend (only required with PHP >= 7.0), cron will send a bunch of emails with errors like:

PHP Warning: error_reporting() has been disabled for security reasons in /var/www/html/wp-load.php on line 24
PHP Warning: error_reporting() has been disabled for security reasons in /var/www/html/wp-includes/load.php on line 333

Some Linux ninja allows us to filter out specific error messages instead of silencing them all (we still want cron to alert us of potential WordPress errors):

* * * * * /usr/bin/php /var/www/html/wp-cron.php 2>&1 | grep -v 'PHP Warning:  error_reporting() has been disabled for security reasons'

WP-CLI based cron

WordPress CLI command line utility can be used to run crons. I have seen this approach mentioned. Not yet sure on the benefits or downsides compared to wp-cron.php:

*/5 * * * * WP_CLI_CONFIG_PATH="$WP_PATH/wp-cli.yml" /usr/local/bin/wp --path="$WP_PATH" cron event run --due-now > /dev/null
  1. Charles Leo

    Just a word of caution here as setting a server-side wp cron can actually cause certain plugins to break. I believe it is rare however it may cause errors down the road. The latest issue I had was with a PHP Plugin checker that looked for PHP 7 compatibility. When the wp-cron was disabled, it caused the plugin to time out.

    • Danila Vershinin

      I’ll necro-answer this comment. Such plugins should be completely eliminated, our of the question they are badly coded.


Leave a Reply

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

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.