Replace WordPress Cron With Linux Cron

WordPress has a built int cron feature that will allow the overall platform and for plugins to run tasks. By default, this cron will be triggered based on user visits. If a user visits the site, the cron will be checked and if a task is due to be executed, it will run and perform whatever task it has been instructed to run. This is generally quite good but the cost of this check still falls on the user that visits the website.

Why would you disable WordPress cron?

If you have a low volume website, the cron tasks may not run due to a lack of visits to the website. You may want to keep plugins up to date and make sure other critical things are kept up to date. This won’t work well if people are not visiting the site to trigger the cron jobs.

On the other end of the scale, if you have a very high volume website, you will have many visitors per second. It is a waste of computing power to have every single visitor be responsible for checking whether cron tasks need to run.

On the other side, we all use cache plugin these days. Bigger sites might even use a reverse proxy. These cache hits will never hit the WordPress code and as a result, will not have the chance to check if cron needs to run. You could get 10’000 hits in an hour and if all of them are cache hits, WordPress’s PHP code will never execute and as a result, cron will not run.

The solution to both of these problems is the same. Disable the default WordPress cron behaviour and make use of the baked-in cron system that comes with all Linux servers.

How To Disable WordPress Cron

Disabling cron is pretty simple. Go to the wp-config.php file in the root of your WordPress install and add the following line of code. This will turn off the default cron functionality.

define('DISABLE_WP_CRON', true);

At this point, no more cron tasks will be executed until you update your Linux crontab to perform the call. The line of code below should be added to your cron files. This is normally found in /etc/crontab but may vary depending on what flavour of Linux you are using.

This entry will make a call to the WordPress cron system every 15 minutes, which is what the default is for WordPress already. You can increase or decrease this as often as you like.
*/15 * * * * root php /var/www/nerdpress/wp-cron.php >/dev/null 2>&1

Advantages of Using Linux Cron For WordPress

The main benefit of using Linux cron for WordPress is a big increase in efficiency. Linux cron is a battle-hardened system that is incredibly efficient. Your operating system will already be using and running this so the extra cost of adding an additional line is very small. The processing will only be performed when needed.

From the PHP side, when a user visits your website, no processing time will be spent checking whether the WordPress cron needs to run. Linux will run it every 15 minutes and no wasted processing will be performed. For very high and very low volume websites, this is one of the best things you can do to squeeze a little more juice out of your web server.

Leave a Reply

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

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