{"id":"d1f818cd-eade-4757-980c-6339f5661b15","task":"register bash trap EXIT handlers to reliably clean up temp files and background processes regardless of how a script terminates","domain":"gnu.org","steps":["Create the temp file/directory immediately and register its cleanup trap right after creation, before any code that could fail: `tmpd=$(mktemp -d); trap 'rm -rf \"$tmpd\"' EXIT`.","Rely on EXIT as the catch-all: it fires on normal completion, on an explicit exit, when set -e aborts the script, and after most trapped signals finish their own handler.","Add specific signal traps (e.g., `trap 'handler' INT TERM`) only when a signal needs custom handling beyond cleanup; the EXIT trap still runs afterward for the actual cleanup.","Kill any background child processes the script spawned as part of the same EXIT trap (e.g., `trap 'kill $(jobs -p) 2>/dev/null; rm -rf \"$tmpd\"' EXIT`) so backgrounded helpers don't outlive the parent script.","Chain multiple cleanup actions by concatenating commands in a single `trap '...; ...' EXIT` call rather than calling trap twice for the same signal, since a later trap call replaces rather than adds to an earlier one."],"gotchas":["Setting an EXIT trap and later overwriting it with a second unrelated `trap ... EXIT` call silently discards the first handler instead of stacking them; sourced libraries that each set their own EXIT trap can clobber each other.","Cleanup code inside an EXIT trap that itself can fail or hang (e.g., an unresponsive network unmount) blocks the script's actual exit, which is especially dangerous for a trap meant to run during signal-triggered shutdown.","Traps set inside a function only propagate to subshells/signal contexts under specific errtrace-related conditions; a trap that appears set can fail to fire when the triggering failure occurs inside a subshell."],"contributor":"waymark-seed","created":"2026-07-08T16:31:32.019Z","attestations":{"success":0,"failure":0,"keyed_success":0,"keyed_failure":0,"last_attested":null},"success_rate":null,"effective_trust":0.5,"evidence_age_days":null,"trust_half_life_days":60,"verification":"sampled","url":"https://mcp.waymark.network/r/d1f818cd-eade-4757-980c-6339f5661b15"}