Replacing WP Cron with Linux Server Cron to Improve WordPress Performance

This quick tutorial will be showing you how to replace wp-cron with the Linux native Cron to improve WordPress performance.

What is WP-Cron

Wp-cron is a pseudo-CRON daemon for scheduling WordPress tasks like automatically updating WordPress, plugins and themes. By default, wp-cron.php runs on every page load to check if there’s tasks to run. If there’s no visitor to a WordPress site, then scheduled WordPress tasks won’t run.

The wp-cron.php script takes 40ms~80ms to finish, as seen in my Nginx access log below.

"07/Apr/2019:08:32:43 +0000" client=74.125.128.103 method=POST request="POST /wp-cron.php?doing_wp_cron=1554625963.4381899833679199218750 HTTP/1.1" request_length=574 status=200 bytes_sent=386 body_bytes_sent=31 referer=https://www.linuxbabe.com/wp-cron.php?doing_wp_cron=1554625963.4381899833679199218750 user_agent="WordPress/4.9.8; https://www.linuxbabe.com" upstream_addr=unix:/run/php/php7.3-fpm.sock upstream_status=200 request_time=0.083 upstream_response_time=0.084 upstream_connect_time=0.000 upstream_header_time=0.084

Replacing WP-Cron with Linux Native Cron

Why replace wp-cron?

  • Running the wp-cron.php script on every page load adds unnecessary load to your server.
  • If there’s no visitor to your WordPress site, then the scheduled tasks won’t run.

Instead of using wp-cron, you can replace it with the Linux server Cron to specify the task intervals, which is way more efficient. First, you need to disable wp-cron by adding the following line in the wp-config.php file in your WordPress installation directory.

define('DISABLE_WP_CRON', true);

This line should added after the following line.

define('DB_COLLATE', '');

disable wp-cron

Once you save the wp-config.php file, wp-cron is disabled. Then we should edit the Linux native Cron task scheduler.

sudo crontab -e

And add the following line.

@hourly sudo -u www-data /usr/bin/php /usr/share/nginx/linuxbabe.com/wp-cron.php

Where:

  • @hourly: Run the command hourly
  • sudo -u www-data: Run as the www-data user.
  • /usr/bin/php: the full path to the PHP binary.
  • Replace the red text with the path to your WordPress directory.

Save and close the Crontab file and your are done. Note that many folks put the following command in Linux system Cron,

wget -q -O - https://yourdomain.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1

But I recommend you should not use the wget utility program to generate HTTP requests, because that will add extra load to your web server. Instead, you should use the PHP binary to run wp-cron.php script directly.

Final Thoughts

Replacing the wp-cron with Linux native cron can reduce write to the Apache/Nginx server access log, which makes your WordPress a bit more faster and your will have a cleaner access log file.

Rate this tutorial
[Total: 2 Average: 5]

Leave a Reply

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