Skip to content

Instantly share code, notes, and snippets.

@bjornjohansen
Last active September 17, 2023 21:12
Show Gist options
  • Save bjornjohansen/a00a9fee5475c4dadb56 to your computer and use it in GitHub Desktop.
Save bjornjohansen/a00a9fee5475c4dadb56 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.
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
@sidati
Copy link

sidati commented Mar 14, 2016

Hi @bjornjohansen,
I had an issue with the script and i can't figue it out whats the problem. the cron keep giving me "WP-CLI is not available" in my log, so i replace the wp with the absolute path /usr/local/bin/wp and still get the same error so i replace it again with phar://wp-cli.phar the error message gone but a new error start displaying WordPress is not installed here path/to/my/wp ???!!!!

Ubuntu OS 14.04
WP-CLI 0.22.0

@KingYes
Copy link

KingYes commented May 29, 2016

@sidati Try my fork code: https://gist.github.com/KingYes/37fecc169cc1be15e7dd9e6e79543467
PS: Make sure you change your php and wp-cli path in the top lines.

@bjornjohansen Nice script, thank you !

@Montano5
Copy link

Montano5 commented Nov 7, 2016

Hi !

Thank you for your script. But I have nearly the same problems as @sidati:

  • First wp-cli doesn't want to be run as root user for security reasons. So I run it with this command for example:
    sudo -u webuser wp --info
    It may be usefull to add a USER variable to launch wp-cli as this user.

  • Second, I also have this "WordPress is not installed here: /path/to/wordpress" despite the fact that I wrote the correct path in the script. We're agree that the PATH is the directory where wordpress is installed and where we can locate wp-config.php and others critical files, right ?

Thank you for your great job ;)

Edit: yes, correcting the user running wp makes no error code displaying (and no more Wordpress is not installed here).
I can't switch to the webuser as the user is not created on the server so command
su webuser
is not working. I can't edit crontab for this user too as I can't switch to it.
So I simply add the command 'sudo -u webuser' before any 'wp' command in your script and everything is going the right way :)

@niemenmaa
Copy link

It would seem that instead of looping through all the jobs and running them even if they weren't due (which this does if i understand correctly), you could just run:

wp cron event run --due-now

Like answered here: https://wordpress.stackexchange.com/a/184113

@erfan-ilyas
Copy link

###
##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