INFRA-DEVOPS Contents

Signals, PID 1, and Graceful Shutdown

Handle signals correctly (PID 1) for graceful shutdowns, avoiding data loss and stuck rollouts.

On this page

Why This Matters

  • Bad signal handling causes stuck rollouts and data loss.
  • PID 1 has special behavior; your app may not receive signals.

Signal Basics (Ops View)

  • SIGTERM: graceful stop request (default in orchestration).
  • SIGKILL: forced termination (after timeout).

Docker Stop Behavior

# sends SIGTERM, then SIGKILL after timeout
docker stop -t 20 <container>

Entrypoint Pattern

# Prefer exec form so signals reach your process
CMD ["node","server.js"]

# If you wrap with a shell script, use exec:
# exec node server.js

Verify in a Container

docker run --rm -d --name sigtest alpine sh -lc 'trap "echo TERM; exit 0" TERM; while true; do sleep 1; done'
docker stop -t 3 sigtest
docker logs sigtest 2>/dev/null || true

Failure Modes

  • PID 1 ignores SIGTERM: app never stops, rollout hangs.
  • Too short timeout: forced kill during writes corrupts state.