I'm confused about forwarding signals to child processes with traps. Say I have two scripts:
a.sh
#!/bin/bash
# print the process id
echo $$
cleanup() {
rv=$?
echo "cleaning up $rv"
exit
}
sleep 5
trap '' SIGTERM # trap cleanup SIGTERM
echo 'cant stop wont stop'
./b.sh
echo 'can stop will stop'
trap - SIGTERM
sleep 4
echo 'done'
b.sh
#!/bin/bash
sleep 4;
echo 'b done'
If I execute a.sh
and then from another window kill the process group with kill -- -PGID
, the SIGTERM is ignored and not passed on to b.sh
. But if I do trap cleanup SIGTERM
, the SIGTERM passes through and terminates b.sh
. Why is my trap passing the signal in one case and not the other?