Don’t let your shell scripts leave junk everywhere when they die
Technical Briefing | 11/22/2026
We all write scripts that create temporary files, launch background processes, or even lock resources. The standard practice? “trap “rm -f $TMPFILE” EXIT”. Most times, that just works. Until it doesn’t. You’re debugging something, hit Ctrl+C, and suddenly there’s a /tmp/my_script_temp.log lingering around, or worse, a rogue process. I’ve seen this bite people in prod, leading to disk fills or port conflicts. It’s an annoying kind of technical debt, the kind that silently accumulates until it’s a real problem.
The default ‘trap EXIT’ just isn’t enough
The `EXIT` trap, bless its heart, only fires when the shell exits normally or when you explicitly call `exit`. But what happens if someone hits `Ctrl+C`? That sends a `SIGINT`. What if an orchestration system decides your script is taking too long and sends a `SIGTERM`? Those signals bypass your `EXIT` trap entirely. And then you’re left with orphaned files, forgotten PIDs, and a mess that you then have to manually clean up. It’s frustrating.
#!/bin/bash
set -euo pipefail
cleanup() {
echo "Cleanup function fired. Removing temp file: $TMPFILE"
rm -f "$TMPFILE"
}
TMPFILE=$(mktemp)
echo "Created temp file: $TMPFILE"
trap cleanup EXIT
echo "Sleeping for 60 seconds. Try Ctrl+C (SIGINT) or 'kill $$' (SIGTERM) in another terminal."
sleep 60
echo "Script finished normally."
Run that script, then open another terminal and run `kill <PID_OF_YOUR_SCRIPT>` (get the PID using `ps aux | grep cleanup.sh`). Or just hit `Ctrl+C`. You’ll notice the `cleanup` function, bound to `EXIT`, never runs. The temporary file stays there. That’s the problem right there.
Catching the real killers: SIGINT and SIGTERM
To make your cleanup truly robust, you need to tell `trap` to listen for those common termination signals. Adding `INT` and `TERM` to your trap list means your cleanup function gets a shot at running even when the script is interrupted. It won’t save you from `SIGKILL` (nothing does, really, that’s the kernel pulling the plug, and it shouldn’t be used unless absolutely necessary), but it covers 99% of graceful and semi-graceful terminations. And make sure your cleanup handler itself is bulletproof — it shouldn’t fail. Think about it: if your cleanup routine bombs out, you’re back to square one.
- Always use `mktemp` for temporary files; it handles uniqueness and permissions securely.
- Combine cleanup actions into a single function, then trap that function for multiple signals (e.g., `trap cleanup EXIT INT TERM`).
- Don’t rely on `EXIT` alone; add `INT` and `TERM` at minimum. You might add `HUP` if your script is susceptible to terminal close.
- Consider the implications of child processes; a `trap` set in the main script won’t necessarily apply to a command run in a subshell, like inside a `$(…)` or `(…)` without propagating signals correctly.
Thinking about how your script dies is just as important as how it runs successfully. A little proactive signal handling means you’re not left scratching your head later wondering why `df -h` looks suspicious or why that port is still in use after you killed your script. It’s a small change with a big payoff in reliability, especially when these scripts are part of a larger automation pipeline.
