WP-Cron using Linux’s Cron

The WordPress default system "virtual" cron requires visitors to execute events. During each page load, it checks the database if there are any scheduled tasks to run. Without visitors, cron will not be prompted. This method is very inefficient and adds additional resource usage to your server.

The best way to optimize the efficiency of your WordPress tasks is to disable WP-Cron and set up a normal cron job. This tutorial will help walk you through replacing WordPress cron with a Linux cron job.

Disable wp-cron

To disable the WordPress default WP-Cron behavior add the following line to your wp-config.php file.

define('DISABLE_WP_CRON', true);

Create a Linux crontab

From your Linux terminal, open your crontab.

crontab -e

Then add one the following lines after making sure to edit your correct path to wp-cron.php.

PHP-CGI

*/10 * * * * curl http://example.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1

PHP-CLI

*/10 * * * * cd /var/www/example.com/public_html; php /var/www/example.com/public_html/wp-cron.php?doing_wp_cron >/dev/null 2>&1

WP-CLI

You can also manage WP-Cron events in WP-CLI. For example, the following command will give you your current cron event list.

wp cron event list

Alternately, you can also use WP-CLI as a crontab to run all due processes.

*/10 * * * * /usr/local/bin/wp cron event run --due-now --quiet --path=/var/www/example.com/public_html

All the above examples will run WP-Cron every 10 minutes. Changing */10 to */5 will execute every 5 minutes. Alternative commands can be found at https://crontab.guru.

PHP-CGI has execution time limits ( --connect-timeout and --max-time ) where as PHP-CLI and WP-CLI do not. Depending on your setup, it may be desirable or undesirable.

Why append >/dev/null 2>&1

  • > is for redirect
  • /dev/null is a black hole where any data is sent, will be discarded
  • 2 is the file descriptor for Standard Error
  • > is for redirect
  • & is the symbol for file descriptor (without it, the following 1 would be considered a filename)
  • 1 is the file descriptor for Standard Out

Therefore >/dev/null 2>&1 is redirecting the output of your program to /dev/null. Including both the Standard Error and Standard Out.

More information is available at The Linux Documentation Project’s I/O Redirection page.

Cron will only email you if there is some output from your job. With everything redirected to null, there is no output so email is disabled.

It is highly recommend running WP-Cron via a Linux crontab rather than WordPress’s default cron.