Help required SIGQUIT in Bash
/tmp/trap.sh
#! /bin/bash
echo parent
trap signalCaught HUP INT QUIT TERM
signalCaught() {
echo "SIGNAL DETECTED I am the parent."
}
SLEEP=10
for i in $(seq $SLEEP -1 0); do
echo "$i"
sleep 1
done
/tmp/trap2.sh 2>&1 | tee -ai /tmp/garbage.txt
echo " --- terminating \"$0\" "
/tmp/trap2.sh
#! /bin/bash
echo child
trap 'echo signal caught in child' HUP INT QUIT TERM
read JUNK
SLEEP=10
echo sleeping for $SLEEP seconds
sleep $SLEEP
echo " --- terminating \"$0\" "
When I run /tmp/trap.sh, and allow it to call trap2.sh, SIGQUIT is only caught by the parent process (trap.sh). The "echo signal caught in child" is not echoed. I assume, then, that the child do开发者_开发知识库es not catch SIGQUIT.
Is there a reason the child does not catch QUIT? It does catch INT
tee -ai /tmp/garbage.txt
is catching SIGQUIT
. So for example, when both trap.sh
and trap2.sh
are running, you have something like:
% pstree 62655
-+= 62655 nicholas -zsh
\-+= 62867 nicholas /bin/bash ./trap.sh
|--- 62889 nicholas /bin/bash /tmp/trap2.sh
\--- 62890 nicholas tee -ai /tmp/garbage.txt
When I press ^\
, it gets delivered to the bottom of the tree (pid 62890):
% sudo dtrace -n 'proc:::signal-send /pid/ { printf("%s -%d %d",execname,args[2],args[1]->pr_pid); }'
dtrace: description 'proc:::signal-send ' matched 1 probe
CPU ID FUNCTION:NAME
1 19556 sigprocmask:signal-send Terminal -3 62890
If I explicitly kill -QUIT 62889
, then it does indeed print signal caught in child
.
(Thanks to the commenter for making me challenge my assumptions: my previous answer here was completely wrong.)
As @Random832 mentions, process groups are used for delivering signals. In pstree output, =
indicates the leader of a process group. You can also see with ps -j
output, trap.sh
, trap2.sh
and tee -ai ...
are in the same group:
% pstree 64261
-+= 64261 nicholas -zsh
\-+= 64551 nicholas /bin/bash ./trap.sh
|--- 64554 nicholas /bin/bash /tmp/trap2.sh
\--- 64555 nicholas tee -ai /tmp/garbage.txt
% ps -jxp 64261,64551,64554,64555
USER PID PPID PGID SESS JOBC STAT TT TIME COMMAND
nicholas 64261 64260 64261 90c3998 1 S s002 0:00.12 -zsh
nicholas 64551 64261 64551 90c3998 1 S+ s002 0:00.01 /bin/bash ./trap.sh
nicholas 64554 64551 64551 90c3998 1 S+ s002 0:00.00 /bin/bash /tmp/trap2.sh
nicholas 64555 64551 64551 90c3998 1 S+ s002 0:00.00 tee -ai /tmp/garbage.txt
精彩评论