Using native Linux cron with WordPress to improve website performance

Using native Linux cron with WordPress to improve website performance

WordPress has a built-in scheduling system. These can be tasks for checking for updates, scheduling publications, and other recurring tasks.

It does not work like a standard system in Linux, but performs tasks when a user loads a page. Thanks to this, the user does not need to configure cron when creating a site. But in turn, this approach has disadvantages:

  • If you need to complete a long task or there are many tasks - the user who visits the site will wait until these tasks are completed before he sees the page.
  • If there is not a lot of traffic on the site, tasks may simply not be completed on time.

This problem can be solved by disabling the built-in WordPress scheduling system and using cron tasks in Linux.

The first thing to do is disable the built-in WordPress scheduling system. To do this, add the following line of code to your wp-config.php:

define('DISABLE_WP_CRON', true);

The second step is to add the task to native Linux cron. Go to server console by SSH and run next:

crontab -e

At this step, you can see a request to select a text editor that is convenient for you (nano, vim, etc.). By specifying the required one, we will see the scheduled task configuration file. Add next line to end of file:

*/ 5 * * * * wget - q - O - 'https://mysite.com/wp-cron.php?doing_wp_cron' > /dev/ null 2 ​​> & 1   

Where mysite.com you have to change to your website domain. In this line we indicate that we need to execute scheduled tasks every 5 minutes.

Save the file and check that the tasks are executing correctly. Use next command if you have root permissions:

grep CRON /var/log/syslog

Or check tasks execution in Tools->Sheduled Actions in admin panel of your WordPress site.

Read more