/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\" "
当我运行/tmp/trap.sh并允许它调用trap2.sh时,SIGQUIT只被父进程(trap.sh)捕获。没有回应“儿童中捕获的回声信号”。我假设,然后,孩子没有抓住SIGQUIT。
孩子是否有理由不接受QUIT?它确实捕获了INT
答案 0 :(得分:3)
tee -ai /tmp/garbage.txt
正在追赶SIGQUIT
。例如,当trap.sh
和trap2.sh
都在运行时,您可以使用以下内容:
% 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
当我按下^\
时,它会被传递到树的底部(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
如果我明确kill -QUIT 62889
,那么确实会打印signal caught in child
。
(感谢评论者让我挑战我的假设:我之前的回答是完全错误的。)
如@ Random832所述,进程组用于传递信号。在pstree输出中,=
表示进程组的领导者。您还可以看到ps -j
输出,trap.sh
,trap2.sh
和tee -ai ...
属于同一群组:
% 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