A cron job is a script or function which is executed at a scheduled time, periodically. They can be once off tasks such as publishing a post or recurring tasks like checking for updates. To help improve performance, WordPress only checks for scheduled items with each page load which is great for low to medium traffic sites, but on particularly busy sites, this constant scanning action can actually slow things down quite considerably.
Understanding how WP-CRON Works
It’s important to understand that WP-CRON does not work the same way a system cron does, it simply mimics it’s functionality. A system cron functions the same way we interact with an alarm. We schedule an item at a specific date and time and are reminded to act at the time, with no further interaction required. WP-CRON however, works more like a silent oven timer. We set a timer and have to keep watching the clock to know when to act. In this analogy, each page load is like the site glancing at the timer to see how long is left.
This works just fine with a steady stream of moderate traffic but consider the two extremes: At one end of the scale, the site gets so much traffic, that it is continuously checking it’s WP-CRON schedule. This adds to the work required by the server for every page load and has a negative impact on performance. Conversely, if there is little to no traffic, the site does not check it’s scheduled items quick enough and may miss things like a scheduled backup from a plugin or a scheduled post.
Streamlining this function is a two-part process. First, we need to create a system cron which will run any scheduled events for us and secondly, we need to stop WP-CRON from constantly checking for those same scheduled events.
Create a System Cron to execute WP Scheduled Events
- Connect to your server via SSH
- Execute
crontab -e
- If no
mailto
line is blank, you will be prompted to enter an email address for output delivery. Leaving this blank means no cron outputs will be delivered. - Select your editor from the installed packages on your server:
Select an editor. To change later, run 'select-editor'.
1. /bin/ed
2. /bin/nano <---- easiest
3. /usr/bin/mcedit
4. /usr/bin/vim.basic
5. /usr/bin/vim.tiny
Choose 1-5 [2]:
- Enter
*/15 * * * * wget -q -O - https://domain.tld/wp-cron.php?doing_wp_cron >/dev/null 2>&1
and save the file. - NOTE: Be sure to update
https://domain.tld
to your actual domain name. - If the crontab updates successfully
/usr/bin/crontab: installing new crontab
will print. (Note, the prefix may be slightly different depending on your filepaths.)
Prevent WordPress from Checking for Scheduled Events on Every Page Load
Now that our system is looking for scheduled events every 15 minutes, we don’t want WordPress to do the same thing on every page load. We can edit the wp-config.php
file with the following line to achieve this:
define('DISABLE_WP_CRON', true) ;
Make sure it’s added before the /* That’s all, stop editing! Happy blogging. */
line.
This only prevents WordPress from checking for scheduled events, but they are still executed as normal when the wp-cron.php file is called directly which is what our system cron now handles.