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

After patching, services must be restarted in order: database, then API, then web, each healthy before the next. Script it.

BeginnerScenarioScenarios: deployments, user management and fleet operations
Answer

Situation read → Ordered restart with a health gate per step, stopping on the first failure.

What I'd do →

declare -A check=(
  [postgresql]='pg_isready -q'
  [api]='curl -fsS -o /dev/null --max-time 3 http://127.0.0.1:8080/readyz'
  [nginx]='curl -fsS -o /dev/null --max-time 3 http://127.0.0.1/'
)
for svc in postgresql api nginx; do
  systemctl restart "$svc"
  for _ in {1..30}; do bash -c "${check[$svc]}" && continue 2; sleep 2; done
  echo "$svc not healthy after restart, stopping" >&2; exit 1
done
echo "all services healthy"
  1. continue 2 jumps to the next service once its check passes.
  2. The array keeps order explicit (associative arrays do not preserve it; the for list does).

Why → Restarting dependents before their dependency is healthy just creates a second wave of errors.

Interview tip → Mention systemd After=/Requires= for boot ordering, and that health gating still needs a check like this.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Scenarios: deployments, user management and fleet operations