-1
#!/bin/sh

trap 'trap - EXIT; echo exit' EXIT
trap 'echo term; exit' TERM

sleep 100 

If I run kill for the PID of the shell process of the script, it does not terminate. If I remove the TERM trap, and do the same, the shell will terminate. Why? Similarly, if I replace sleep 100 by sleep 100 & wait, the shell will terminate by kill.

1 Answer 1

-1

The shell checks for outstanding traps only at specific checkpoints (at the end of a command) and when it is implicitly waiting for a running command, it is just before such a checkpoint.

On the other side: if you do not use trap .... TERM, the signal is used with default behavior which is to terminate the process (even the shell).

If you use trap .... TERM, you no longer have the default signal behavior for SIGTERM and this prevents the shell from being terminated.

1
  • I do not understand this: "and when it is implicitly waiting for a running command, it is just before such a checkpoint." What do you mean by implicitly waiting? What is just before such a checkpoint?
    – jarno
    Jul 5, 2020 at 22:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .