Skip to content

Instantly share code, notes, and snippets.

@soderlind
Forked from bjornjohansen/run-wp-cron.sh
Last active February 23, 2022 20:16
Show Gist options
  • Save soderlind/bc28c6dc82fbf01f9cd539f470d504af to your computer and use it in GitHub Desktop.
Save soderlind/bc28c6dc82fbf01f9cd539f470d504af to your computer and use it in GitHub Desktop.
Run all due cron events for WordPress with WP-CLI. Works with both single sites and multisite networks.
#!/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.
# Modified by Per Søderlind
WP_PATH="/path/to/wp"
MAIN_SITE="http://www.domain.tld" # --url="$MAIN_SITE" below, prevents the Undefined index: HTTP_HOST error.
# 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" --url="$MAIN_SITE" --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" --url="$MAIN_SITE" --quiet --network);
then
SITE_URLS=`wp site list --fields=url --archived=0 --deleted=0 --format=csv --path="$WP_PATH" --url="$MAIN_SITE" | sed 1d`
else
SITE_URLS=`wp option get siteurl --path="$WP_PATH" --url="$MAIN_SITE"`
fi
# Loop through all the sites
for SITE_URL in $SITE_URLS
do
# Run all event hooks that are due
wp cron event run --due-now --url="$SITE_URL" --path="$WP_PATH"
done
@erfan-ilyas
Copy link

erfan-ilyas commented Feb 23, 2022

###
##For horizontal scaled wp environment use this to divide load on each node equally. I am using crudini to read the environment values
##This script is not perfect. You might have to modify it for you to work.
###

MAXSERVERS=$(crudini --get ../env-vars-dynamic.ini '' MAX_SERVERS_IN_THIS_NETWORK)
THISSERVERIS=$(crudini --get ../env-vars-dynamic.ini '' THIS_SERVER_NODE_NUMBER)


WP_PATH="/path/to/wp"
MAIN_SITE="http://www.domain.tld" # --url="$MAIN_SITE" below,  prevents the Undefined index: HTTP_HOST error.



# 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" --url="$MAIN_SITE" --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"  --url="$MAIN_SITE" --quiet --network);
then
	SITE_URLS=`wp site list --fields=url --archived=0 --deleted=0 --format=csv --path="$WP_PATH"  --url="$MAIN_SITE" | sed 1d`
else
	SITE_URLS=`wp option get siteurl --path="$WP_PATH"  --url="$MAIN_SITE"`
fi


#run the cron on the main site for once only on 1st node.
if [ $THISSERVERIS == 1 ]; then

	printf "Running on Main site\n"
        wp cron event run --due-now --path="$WP_PATH"
	
fi

# Loop through all the sites
for SITE_URL in $SITE_URLS
do

        MD5HASH=$(echo -n $SITE_UR | md5sum | cut -c 1-1)
        RETURNEDVALUE=$(( ( ($MD5HASH + 1) % $MAXSERVERS) + 1 ))
        
       if [ $RETURNEDVALUE != $THISSERVERIS ]; then
		printf "\nSkipping this subsite as it will be processed by another server node: $SITE_UR \n"
                continue
        fi

	# Run all event hooks that are due
	wp cron event run --due-now  --url="$SITE_URL" --path="$WP_PATH"
done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment