Mehdi Akiki
Published on

Short engineering note

Today I Learnt: Trap signals and cleanup

Authors
  • Mehdi Akiki avatar
    Name
    Mehdi Akiki
    Twitter

Note · Engineering notes

Create the temporary file before registering the cleanup handler:

tmpfile=$(mktemp)
trap 'rm -f "$tmpfile"' EXIT

Use a function when cleanup needs more than one command:

cleanup() {
  rm -f "$tmpfile"
}

trap cleanup EXIT INT TERM