Skip to content

Instantly share code, notes, and snippets.

@KingYes
Forked from bjornjohansen/run-wp-cron.sh
Last active December 31, 2019 07:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KingYes/37fecc169cc1be15e7dd9e6e79543467 to your computer and use it in GitHub Desktop.
Save KingYes/37fecc169cc1be15e7dd9e6e79543467 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="/your/wp/path/"
WP_CLI="/usr/local/bin/php /usr/local/bin/wp"
# Check if WP-CLI is available
if ! hash $WP_CLI 2>/dev/null; then
echo "WP-CLI is not available"
exit
fi
# If WordPress isn�~@~Yt installed here, we bail
if ! $(${WP_CLI} 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_CLI core is-installed --path="$WP_PATH" --quiet --network);
then
SITE_URLS=`${WP_CLI} site list --fields=url --archived=0 --deleted=0 --format=csv --path="$WP_PATH" | sed 1d`
else
SITE_URLS=(`${WP_CLI} 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_CLI} cron event list --format=csv --fields=hook,next_run_relative --url="$SITE_URL" --path="$WP_PATH" | grep now$ | awk -F ',' '{print $1}')
do
${WP_CLI} cron event run "$EVENT_HOOK" --url="$SITE_URL" --path="$WP_PATH" --quiet
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment