Managing Active Cron Jobs

To ensure that your background WordPress tasks (scheduled posts, checking for plugin and theme updates, etc.) are actioned precisely when they should, SpinupWP configures a server-based scheduled task using the Linux cron service. This scheduled cron task uses WP-CLI to activate WP-Cron, which is the WordPress tool for checking for and processing scheduled tasks.

Usually, WP-Cron is only activated by visitors to your site, which is why SpinupWP uses the server cron to trigger any scheduled WP-Cron tasks. A server cron is configured for each site and set to run at 1-minute intervals.

Cron-Related CPU Spikes

If you have a large number of sites on your server, or you have a large number of scheduled tasks across each site on your server, this could lead to the cron tasks causing CPU spikes. A CPU spike would happen when the cron tasks for all sites on a server are triggered simultaneously, which activates all the scheduled WordPress tasks and leads to higher than usual CPU usage for a while. This high CPU usage could prevent your server from processing regular web requests, leading to delays and possibly timeouts for your website visitors.

One way to avoid this problem is to decrease the cron frequency for any sites that don’t need to activate scheduled tasks on a strict schedule by configuring different cron intervals. For example, if you have a site that must publish posts on a specific schedule every time, leave that at the default of 1 minute. If you have sites that rarely have scheduled posts or don’t post anything on a schedule at all, you could increase the interval to every 15 minutes.

Editing a Site Cron Interval

To edit the cron interval for a site, SSH into the server using the specific site user. Open the crontab using the following command:

crontab -e

If you get asked to choose an editor, hit 1 to select /bin/nano.

Select an editor.  To change later, run 'select-editor'.
  1. /bin/nano        <---- easiest
  2. /usr/bin/vim.basic
  3. /usr/bin/vim.tiny
  4. /bin/ed

Once the crontab is open, find the cron command for the site. It should look something like this:

*/1 * * * * cd /sites/; /usr/local/bin/wp cron event run --due-now >/dev/null 2>&1

Change the minute interval from /1 to /5, which updates it from 1 to 5 minutes.

*/5 * * * * cd /sites/; /usr/local/bin/wp cron event run --due-now >/dev/null 2>&1

You could also increase it to 10, or 15, or even 30 minutes.

Once you've made the change, hit Ctrl + X and Y to save the changes.

Staggering Cron Jobs

For more control, it is possible to stagger your cron jobs, by working with the step values for any of the cron time and date fields.

Let’s look at a simple example. A * in the minute field can also be represented as 0-59/1, where 0-59 is the range (minute 0 to minute 59) and 1 is the step value.

0-59/1 * * * * 

This command will run at the first minute in the range (0), then at all successive minutes that are distant from the first by step (1), until the last minute (59). Essentially, every 1 minute starting at minute 0 and ending at minute 59.

Earlier we showed you how to run a cron every 5 minutes

*/5 * * * *

Which is the same as

0-59/5 * * * *

Which will run the command every 5 minutes, starting at minute 0 and ending at minute 59 (eg 5, 10, 15, 20, etc, up to 55, but not 59).

Because hours are divided into 60 minutes, setting a step value that divides evenly into 60 (1, 5, 20) means that the cron runs at every value of the step (every 1, 5, or 20 minutes) and continues to do so for every consecutive hour. In the two previous examples the command will run 12 times during the 60 minutes of an hour, and then again 12 times for all following hours. It also means that the last time it runs in a previous hour (minute 55) is also exactly 5 minutes away from the next time it runs in the next hour (minute 0).

Setting a step value to something that does not divide evenly into 60 means that the intervals will change on the hour.

0-59/25 * * * *  /some/command

In this example, in the first hour, the cron will run 3 times, first at minute 0, then at minute 25, and then at minute 50. Then it will run 10 minutes later, at minute 0 of the next hour, which is not the same as running every 25 minutes.

With this knowledge, it’s possible to offset a cron start time, by specifying the range explicitly and setting the step value to the amount of the offset.

For example, to run a command at every 20th minute from 5 through 59.

5-59/20 * * * * 

This will run at minute 5, and then at minute 25, and then at minute 45 of every hour.

To run a command at every 25th minute from 10 through 59.

10-59/25 * * * * 

This will run at minute 10, and then minute 35 of every hour.

This can also be expanded to the rest of the fields. For example to run a command at minute 5 of every 10th hour from 0 through 24.

5 0-23/10 * * *

This will run at 00:05, then 10:05, and then 20:05.

If you’re not sure if your cron fields are configured correctly, a tool like crontab guru is very useful for testing cron field combinations.

If you don't want to make changes to the cron, you might want to consider moving some of the sites to a different server.