Alexander Mills
Alexander Mills

Reputation: 99959

bash trap is not working for SIGTERM or SIGKILL

I have this:

  trap on_ql_trap EXIT;
  trap on_ql_trap INT;
  trap on_ql_trap TERM;
  echo "pid that called trap: $$"

which can probably be turned into shorthand:

  trap on_ql_trap EXIT INT TERM;
  echo "pid that called trap: $$"

when I kill the process/pid that called trap, using

kill <pid>

or

kill -9 <pid>

in the first case, nothing happens. The process lives on. In the second case using -9, the process dies, but the trap is not invoked. So neither kill command is doing what I want! I want the process to exit but I need to trap to be invoked first. Does anyone know why this might be?

Upvotes: 0

Views: 4368

Answers (1)

David C. Rankin
David C. Rankin

Reputation: 84521

As you have discovered there are two signals that cannot be caught, blocked or ignored. They are SIGKILL and SIGSTOP. The reference describing the limitation is man 7 signal. It spells it out in unambiguous terms:

The signals SIGKILL and SIGSTOP cannot be caught, blocked, or
ignored.

That limitation applies to the bash trap builtin.

Upvotes: 7

Related Questions