77 / 80 · 12 Programming for QA · Bash Scripting for QA← prev⊞ allnext →☰ Read as one page
9.7Trap for Cleanup
Ensure cleanup runs even if the script crashes:
#!/bin/bash
set -euo pipefail
cleanup() {
echo "Cleaning up..."
kill $SERVER_PID 2>/dev/null || true
docker compose down 2>/dev/null || true
}
# Register cleanup to run on EXIT, regardless of how the script ends
trap cleanup EXIT
# Now start things up — cleanup is guaranteed to run
docker compose up -d
SERVER_PID=$!
pytest tests/