Interview Prep/Shell Scripting/Scenarios: deployments, user management and fleet operations

Run a command on 200 hosts in parallel and produce a summary of which succeeded and failed.

IntermediateScenarioScenarios: deployments, user management and fleet operations
Answer

Situation read → Bound concurrency, add timeouts, capture per-host output and exit codes.

What I'd do →

out=$(mktemp -d)
run_one() {
  local h=$1
  if timeout 60 ssh -n -o BatchMode=yes -o ConnectTimeout=5 "$h" 'sudo systemctl is-active app' > "$out/$h.log" 2>&1
  then echo "$h OK"; else echo "$h FAIL($?)"; fi
}
export -f run_one; export out
xargs -a hosts.txt -P 20 -I{} bash -c 'run_one "$1"' _ {} | sort > "$out/summary.txt"
grep -c ' OK' "$out/summary.txt"; grep FAIL "$out/summary.txt" || true
  1. -P 20 limits parallel SSH sessions; -n stops SSH from reading stdin.
  2. Pass the host as an argument (_ {}), never into the command string.

Why → You get a reproducible summary and per-host logs for the failures, without hammering the network.

Interview tip → Mention Ansible ad-hoc (ansible all -m shell -a ... -f 20) or pssh as ready-made alternatives.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Scenarios: deployments, user management and fleet operations