Skip to content

WordPress WP-CLI batch operations across a fleet of sites

Running one WordPress WP-CLI command against one site is easy. Running the same command against 40 sites with rollback, per-host failure handling, and a usable report needs a different shape.

Running wp plugin update wordfence on one WordPress site is easy. Running it across a fleet of 40 client sites needs a different shape: per-host failure handling, a usable report at the end, and a way to roll back the three that broke without rolling back the 37 that worked. A for loop over ssh client$i is the wrong primitive — it stops on the first failure and tells you nothing useful.

What’s happening

WordPress WP-CLI’s @all alias group runs sequentially against every alias and stops on the first non-zero exit. That’s safe for queries (wp @all core version) but wrong for mutations — one site with a stuck migration shouldn’t block the other 39 from getting a security patch. The fleet pattern is to drive the loop yourself, capture each host’s stdout, stderr and exit code separately, and report at the end.

The second concern is concurrency. SSH connections are cheap to open, but spinning 40 simultaneous WP-CLI processes against the same Composer-managed plugin mirror saturates the network and confuses the package cache. Two to four at a time is the right shape.

How to verify

Before any fleet mutation, snapshot the current state per site:

for site in $(wp cli alias list --field=key | grep -v '@all'); do
  echo "=== $site ==="
  wp $site core version 2>/dev/null
  wp $site plugin list --format=csv 2>/dev/null > /tmp/fleet/$site-plugins.csv
done

That gives you a diffable starting point. After the fleet operation, run the same loop and diff against the snapshot — any site missing from the post-run output is a site that failed mid-operation.

Confirm one site works in isolation before running across the fleet:

wp @client1 plugin update wordfence --dry-run

--dry-run on wp plugin update reports what would change without writing.

What to do

Drive the loop with GNU parallel and capture everything per host.

  1. Build the alias inventory and a working directory:
mkdir -p /tmp/fleet-run
wp cli alias list --field=key | grep -v '@all' > /tmp/fleet-run/sites.txt
  1. Run the WordPress WP-CLI mutation across the fleet with two parallel workers:
parallel -j2 --joblog /tmp/fleet-run/jobs.log \
  --results /tmp/fleet-run/output \
  "wp {} plugin update wordfence" \
  :::: /tmp/fleet-run/sites.txt

--joblog writes one row per host with exit code and elapsed time. --results writes stdout and stderr to per-host files under /tmp/fleet-run/output/. After the run, the report is one shell command:

awk 'NR>1 && $7!=0 {print $9, "exit", $7}' /tmp/fleet-run/jobs.log

That lists only the sites that failed. The successful ones don’t need attention.

  1. For each failed site, investigate one at a time:
cat /tmp/fleet-run/output/1/@client3/stderr
wp @client3 plugin status wordfence
  1. Rollback policy. If a plugin update breaks a site, the cleanest revert is the previous version pinned by Composer (for Bedrock fleets — see Composer for WordPress) or wp plugin install wordfence --version=7.10.2 --force on managed-WordPress installs.

  2. For multi-step operations (drain cron, update plugin, run search-replace, verify), wrap each site’s run in a script that exits at the first internal failure:

#!/bin/bash
set -euo pipefail
site=$1
wp $site cron event run --due-now
wp $site plugin update wordfence
wp $site cache flush
wp $site option get siteurl  # smoke test

Then drive it: parallel -j2 ./fleet-update.sh :::: sites.txt.

  1. Time-box the fleet run. If the parallel pool hasn’t finished in 30 minutes, something is wrong on the slow hosts — kill the run and audit the joblog rather than waiting for the long tail.

Stack Harbor runs these fleet operations under enterprise WordPress hosting, including the inventory, the per-host snapshots, and the rollback procedure. If a portfolio is stuck running plugin updates by hand because the team doesn’t trust an automated path, we can build the fleet workflow.