Pcornel
Automation · Bash Automation

Trap

Cheat sheet · https://pcornel.dk/topic.php?topic=5285

Back to the Topic

Answer

trap runs commands when a script receives a signal or exits, such as trap 'rm -f "$tmp"' EXIT, so temporary files are cleaned up and locks released even on failure.

Remember

  • trap 'cleanup' EXIT runs on any exit.
  • Trap INT and TERM for signals.
  • Remove temp files and locks.

First checks

  • trap 'cleanup' EXIT — always cleanup
  • trap 'echo "error at line $LINENO" >&2' ERR — error context
  • trap -p — list traps

Verify in this order

signal/exit → trap handler → cleanup

Where it usually breaks

Input/desired state → request accepted → job/task terminal status → target object read-back → effective runtime state → original workload → regression/rollback readiness.

Read-only verification commands unless stated otherwise. Confirm syntax for your platform and release. Full reference: https://pcornel.dk/topic.php?topic=5285

Confirmation

Confirm action

Continue?