๐ง 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 terminationSIGKILLโ forced termination (cannot be trapped)SIGHUPโ terminal closed / reloadEXITโ pseudoโsignal triggered on script exit
2. Traps
A trap registers a handler:
1 2 | |
3. EXIT Trap
Runs regardless of how the script ends (success, error, signal):
1 | |
4. Propagation
Signals propagate through pipelines and subshells, but behavior differs across shells.
๐ง Notable Techniques
Cleanup on Exit
1 2 3 4 | |
Graceful Shutdown
1 | |
Handling Ctrl+C
1 | |
Temporary Resource Management
1 2 | |
Ignoring Signals
1 | |
โ ๏ธ Limitations & Pitfalls
SIGKILLcannot be trapped- Traps behave differently in subshells
- Some shells reset traps inside pipelines
set -einteracts 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
SIGKILLbehavior - 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 (
130for Ctrl+C) - Avoid complex logic inside traps
- Document which signals your script handles
- Test behavior under multiple shells
- Prefer
trap 'handler' INT TERM EXITfor robust shutdown - Avoid relying on traps inside pipelines or subshells
๐งช Testing Traps & Signals
1 2 | |
Test termination:
1 | |
Test cleanup:
1 2 | |
๐ง 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.