Przejdลบ do treล›ci

๐Ÿง  Traps & Signals

Signals are the primary mechanism the operating system uses to interrupt, terminate, or notify processes. Traps allow shell scripts to intercept these signals and perform cleanup, logging, or controlled shutdowns. Understanding traps is essential for writing robust, productionโ€‘grade automation.


๐ŸŽ“ Who This Is For

  • Engineers writing longโ€‘running or stateful scripts
  • DevOps/SRE teams managing daemons, cron jobs, and CI/CD tasks
  • Sysadmins handling cleanup, rollback, or resource management
  • Anyone needing predictable shutdown behavior in shell scripts

๐Ÿงฉ Role in the Ecosystem

Signals are used by:

  • The OS to terminate processes (SIGTERM, SIGKILL)
  • Users (Ctrl+C โ†’ SIGINT)
  • Supervisors (systemd, Docker, Kubernetes)
  • Shells to propagate pipeline failures
  • Scripts to coordinate child processes

Traps allow scripts to intercept these signals and run custom logic before exiting.


๐Ÿงฉ Key Concepts

1. Signals

Common signals:

  • SIGINT โ€” interrupt (Ctrl+C)
  • SIGTERM โ€” polite termination
  • SIGKILL โ€” forced termination (cannot be trapped)
  • SIGHUP โ€” terminal closed / reload
  • EXIT โ€” pseudoโ€‘signal triggered on script exit

2. Traps

A trap registers a handler:

1
2
trap 'cleanup' EXIT
trap 'echo interrupted; exit 130' INT TERM

3. EXIT Trap

Runs regardless of how the script ends (success, error, signal):

1
trap 'rm -f "$TMPFILE"' EXIT

4. Propagation

Signals propagate through pipelines and subshells, but behavior differs across shells.


๐Ÿ”ง Notable Techniques

Cleanup on Exit

1
2
3
4
cleanup() {
  rm -f "$TMPFILE"
}
trap cleanup EXIT

Graceful Shutdown

1
trap 'echo "Stopping..."; kill "$child"; exit 0' TERM

Handling Ctrl+C

1
trap 'echo "Interrupted"; exit 130' INT

Temporary Resource Management

1
2
TMPDIR="$(mktemp -d)"
trap 'rm -rf "$TMPDIR"' EXIT

Ignoring Signals

1
trap '' HUP

โš ๏ธ Limitations & Pitfalls

  • SIGKILL cannot be trapped
  • Traps behave differently in subshells
  • Some shells reset traps inside pipelines
  • set -e interacts poorly with traps in some shells
  • EXIT traps may not run if the shell crashes
  • Bash and dash differ in how they propagate signals in pipelines

๐Ÿง  When to Use Traps

  • Cleaning up temporary files or directories
  • Stopping background jobs or child processes
  • Releasing locks or semaphores
  • Ensuring graceful shutdown in CI/CD
  • Handling user interrupts (Ctrl+C)
  • Managing longโ€‘running scripts or daemons

โŒ When Not to Use Traps

  • When relying on SIGKILL behavior
  • When cleanup must be guaranteed even on kernelโ€‘level termination
  • When portability across shells is uncertain
  • When the script is extremely shortโ€‘lived

โœ… Best Practices

  • Always clean up temporary files via trap 'rm -f ...' EXIT
  • Use explicit exit codes (130 for Ctrl+C)
  • Avoid complex logic inside traps
  • Document which signals your script handles
  • Test behavior under multiple shells
  • Prefer trap 'handler' INT TERM EXIT for robust shutdown
  • Avoid relying on traps inside pipelines or subshells

๐Ÿงช Testing Traps & Signals

1
2
trap 'echo "INT received"' INT
sleep 10

Test termination:

1
kill -TERM $$

Test cleanup:

1
2
TMPFILE=$(mktemp)
trap 'rm -f "$TMPFILE"' EXIT

๐Ÿง  Summary

Traps and signals provide controlled shutdown, cleanup, and error handling for shell scripts. They are essential for writing reliable automation, especially in longโ€‘running or stateful environments. Used correctly, traps ensure predictable behavior even under interruption or termination.