Home Articles Platform info WordPress How to setup server-side cron for WordPress Multisite

How to setup server-side cron for WordPress Multisite

When WordPress is setup as a Multisite/network installation, the cron event process needs to be called for each individual site in the network. This can either be manually setup or solved using a simple script. This script was originally created by Bjørn Johansen.

Time needed: 10 minutes.

Setup server-side cron for WordPress Multisite

  1. Disable WP-cron in WordPress

    Disable the built-in WP-cron process by editing wp-config.php and add (or uncomment if it already exists) this line:

    define( 'DISABLE_WP_CRON', true );

    Save and close the file.

  2. Create a script for WP Multisite

    Create a new file named run-wp-cron.sh and add the following:

    #!/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="/path/to/wp"

    # 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

    # 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
    wp cron event run "$EVENT_HOOK" --url="$SITE_URL" --path="$WP_PATH" --quiet
    done
    done


    Note that you need to set WP_PATH to the full path of your WordPress site installation, ie. /kunder/group/user/public/.

    Save and close the file.

  3. Make the script executable

    Run this command to make the script executable:

    chmod +x run-wp-cron.sh

    You can also test the script like this:

    ./run-wp-cron.sh

  4. Setup the cron job in our control panel

    Go to your WordPress site cron settings page in our control panel and enter the following in the time field:

    */30 * * * *

    This will make the cron job run once every 30 minutes. You can set it to be more frequent, but note that larger WordPress Multisite installations may require some time for all cron processes to complete.

    In the command field, enter the following:

    /kunder/group/user/public/run-wp-cron.sh

    Replace the path with the full path to your WordPress site installation. You can find the full path in the site settings in your Servebolt Control Panel.

  5. Optionally, turn off email notifications

    If you don’t need an email notifications for when cron jobs have been executed, you can select the–newly created–cron job in the list of this page and select “Do not send email” in the drop-down box below. Click the “Update” bottom on the bottom of the page.

Note: if you still receive emails regarding cron jobs even after silencing them, then there’s something wrong with one/more cron events. To get rid of these email, solve the actual error with the cron events.

Did this help you?