Basically I want a bash script process, upon receiving a SIGINT
, to kill all of its subprocesses before exiting. I read here to use something along the lines of:
trap "kill -TERM -$$ ; exit 1" INT QUIT
I have also read to use kill 0
:
trap "kill -TERM 0 ; exit 1" INT QUIT
What are the differences between them, and do they fulfill the following requirements (and if not, what does?)?:
- Kills only itself and its children (assuming it is the top level process started by a terminal command).
- It does not kill any other processes or instances of the same shell script (run from a separate terminal command: i.e. when running
./foo.sh &
and then./foo.sh
, killing the second one shouldn't kill the first one even in the same tty).