How to Setup External Cron Jobs in WordPress for Performance

timer

What are Cron Jobs?

Cron is software in Linux OS to schedule time-based jobs. For example, check for emails, create backups, check for updates, send error logs to 3rd party providers etc.

Cron is run by ‘crontab’ (cron table), a file with configurations of the jobs to run.

Below is the example of a job in the crontab file:

00 11 * * * /home/backups/scripts/log_backup.sh

This tells the OS to run the ‘log_backup.sh’ every day at 11:00. Here is how it works in detail:

# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of the month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday)
# │ │ │ │ │
# │ │ │ │ │
# │ │ │ │ │
# * * * * * command to execute

Why WordPress needs Cron Jobs?

You might have already guessed it!

Here are a few use cases where WordPress itself or some plugins use cron jobs:

  • Check for plugins/themes/core updates
  • Scheduled blog posts
  • Create backups
  • Optimize database tables
  • Send emails
  • and much more…

What’s wrong about inbuilt WordPress Cron?

WP-Cron is Cron for WordPress. However, the working of WP-Cron is different than the cron that I mentioned above.

There are some technical difficulties for WordPress to set cron jobs in the crontab file. So WP-Cron is not based on the server cron. Instead, it checks for any jobs every time a user visits your posts or pages and execute it.

Zero visitors = Zero Cron runs

You scheduled to send an email report (via some plugin) every day at 6 pm. But what if there is no visitor at that time?

More visitors = Unnecessary Cron runs

Suppose your site is receiving 10 pages views every second. Then WP-Cron checks for cron jobs 10 times every second.

Checking and running cron jobs involves PHP execution. As you know it’s resource hungry and will make your site slow as your traffic increases.

A better approach is to use you the above-mentioned system’s cron job and disable WP-Cron.

How to disable WordPress inbuilt Cron Job?

Before we set up external cron jobs, let’s disable the inbuilt WP-Cron.

Open wp-config.php from your file manager and add the following code on top of the file:

define('DISABLE_WP_CRON', true);

How to setup external Cron Jobs in WordPress?

We’ve to call https://domain.com/wp-cron.php?doing_wp_cron every x mins. There are different ways to set it up based on your hosting.

Using EasyCron (easiest way)

EasyCron is a service that lets you create cron jobs easily. The free plans let you create cron jobs that run every 20 mins. That’s more than enough for most of the sites.

easycron wordpress setup

Using cPanel

Open ‘Cron Jobs’ from cPanel:

cron job cpanel

Add a new cron job as follows:

add cron job cpanel

For the command, use the following code (replace domain.com with yours):

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

In a VPS server

SSH into your VPS server and enter the command:

crontab -e

This will open the crontab file. Now add the following command into that file and exit (:wq to exit editor):

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

Closte

cron jobs closte

Kinsta

SSH into the server and follow the steps in ‘In a VPS server’. You can also contact support to do this.

Cloudways

Open your application and goto to ‘Cron Job Management’ to create a new cron job.

Select the type as ‘PHP’ and add ‘wp-cron.php?doing_wp_cron’ to the command:

cloudways cron job

SiteGround

Follow the steps in ‘Using cPanel’ as above.

Conclusion

Enabling external cron jobs help to reduce resource usage, give better performance and run jobs more reliably and on time.

Comment below if you’ve any queries or feedback. I read and reply back to each of them within 12 hours!

You May Also Like