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

Write the core of a deploy script with release directories, an atomic symlink switch and rollback.

IntermediateScenarioScenarios: deployments, user management and fleet operations
Answer

Situation read → The "Capistrano layout" gives zero-downtime switching and instant rollback.

What I'd do →

base=/srv/app; release=$(date -u +%Y%m%dT%H%M%SZ); dir="$base/releases/$release"
mkdir -p "$dir"
tar -xzf "/tmp/app-$version.tgz" -C "$dir"
ln -sfn "$base/shared/.env" "$dir/.env"
previous=$(readlink -f "$base/current" || true)

ln -sfn "$dir" "$base/current.tmp" && mv -T "$base/current.tmp" "$base/current"   # atomic switch
sudo systemctl reload app

if ! wait_for_http http://127.0.0.1:8080/readyz 60; then
  echo "health check failed, rolling back to $previous" >&2
  ln -sfn "$previous" "$base/current.tmp" && mv -T "$base/current.tmp" "$base/current"
  sudo systemctl reload app
  exit 1
fi
  1. ln -sfn alone unlinks then creates; mv -T of a temp link is a true atomic rename().
  2. Keep the last N releases for rollback (see the cleanup in Q26).

Why → All preparation happens off to the side; the only live change is one rename.

Interview tip → Explain why mv -T matters: without it, mv would move the link into the existing current directory.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Scenarios: deployments, user management and fleet operations