Skip to content

Instantly share code, notes, and snippets.

@ph33nx
Created July 14, 2024 13:04
Show Gist options
  • Save ph33nx/ee52fc9cc036b4924700ffb33465510f to your computer and use it in GitHub Desktop.
Save ph33nx/ee52fc9cc036b4924700ffb33465510f to your computer and use it in GitHub Desktop.
Run Cron Jobs in WordPress Using WP CLI (bash Script)
#!/bin/bash
# Run cron jobs for all wordpress sites in a folder using WP CLI.
# Author: https://github.com/ph33nx
# !IMP: Change BASE_DIR below to the folder that has the wordpress sites. Ex: /var/www or /usr/share/nginx etc..
BASE_DIR="/path_to_folder_that_has_wordpress_sites/"
# Check for wp-cli.phar existence in the system
if ! command -v wp &> /dev/null; then
echo "WP-CLI could not be found. Please install it first."
exit 1
fi
# Loop through each WordPress directory
for dir in $BASE_DIR*; do
if [ -d "$dir" ] && [ -f "$dir/wp-config.php" ]; then
echo "Running cron in $dir"
sudo -u www-data wp --path="$dir" cron event run --due-now
else
echo "Skipping $dir, no valid WordPress installation found."
fi
done
echo "All Cron jobs run successfully"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment