Set up server side cron for a WordPress multisite

When WordPress is set up as a multisite or a network installation, the cron event process needs to be called for each individual site in the network. With the latest version of Servebolt Optimizer this is very easy to set up. Alternatively a script can be used which we will go over later in the article.

Using Servebolt Optimizer

  1. Be sure to have the Servebolt Optimizer plugin installed on your site and network activate it throughout your multisite.
  2. Log into your site using SSH. SSH can directly be used by the terminal in Mac / Linux, or with a program like PuTTY with Windows. You can find the login credentials under your site’s Server Login Information. The SSH/SFTP password can be changed on the same page.
  3. Then simply run this command:
wp servebolt cron enable --path=$HOME/public

This will do two things. First it will disable the normal WordPress cron scheduler and set up it’s own Linux cron job on your server. You can check the cron job in under your site’s cron tab in our Control Panel, and you can now see that you will get a green mark in our plugin.

Servebolt Optimizer General Performance tab
Servebolt Optimizer General Performance tab

Using a script

Sometimes using a manual script is more preferable if you for some reason can’t use our plugin. Follow these steps to set it up:

  1. Log into your site using either SSH or SFTP.
  2. Add define( 'DISABLE_WP_CRON', true ); to your wp-config.php file. Or disable the default WordPress cron scheduler by running this command:
wp config set DISABLE_WP_CRON true --raw --path=$HOME/public
  1. Next is to create the script file in your user root directory. You can call it whatever you like but it must end in .sh. Example: run-wp-cron.sh
  2. Copy and paste this code block into that file. The script is originally made by Bjørn Johansen and modified by Servebolt:
#!/bin/bash
# Copyright © 2015 Bjørn Johansen
# This work is free. You can redistribute it and/or modify it under the
# terms of the Do What The Fuck You Want To Public License, Version 2,
# as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.

WP_PATH="$HOME/public"

# Check if WP-CLI is available
if ! hash wp 2>/dev/null; then
    echo "WP-CLI is not available"
    exit
fi

# If WordPress isn’t installed here, we bail
if ! $(wp core is-installed --path="$WP_PATH" --quiet); then
    echo "WordPress is not installed here: ${WP_PATH}"
    exit
fi

# Get a list of site URLs
if $(wp core is-installed --path="$WP_PATH" --quiet --network);
then
    SITE_URLS=`wp site list --fields=url --archived=0 --deleted=0 --format=csv --path="$WP_PATH" | sed 1d`
else
    SITE_URLS=(`wp option get siteurl --path="$WP_PATH"`)
fi

mkdir -p ~/.cron-lockfiles/

# Loop through all the sites
for SITE_URL in $SITE_URLS
do
    # Run all event hooks that are due
    for EVENT_HOOK in $(wp cron event list --format=csv --fields=hook,next_run_relative --url="$SITE_URL" --path="$WP_PATH" | grep now$ | awk -F ',' '{print $1}')
    do
      	FLOCK_INDICATOR=$(echo -n "$SITE_URL-$EVENT_HOOK" | md5sum | awk '{print $1}')
        flock -n ~/.cron-lockfiles/.wp_cron_$FLOCK_INDICATOR.lock wp cron event run "$EVENT_HOOK" --url="$SITE_URL" --path="$WP_PATH" --quiet
    done
done
  1. Lastly add the cron job to our Control Panel:
    • Time interval: */10 * * * *
    • Command: sh run-wp-cron.sh (Be sure to change out the script file name if you’ve chosen another one)
  2. It should look something like this:
Add cron job panel showing */10 * * * * as time and sh run-wp-cron.sh as command
Add cron job panel

And as always, should you have any additional questions please don’t hesitate to contact our support chat at servebolt.com!

Give us your feedback on this article