在子/后台进程中运行时,我无法捕获信号.
这是我简单的bash脚本:
#!/bin/bash
echo "in child"
trap "got_signal" SIGINT
function got_signal {
echo "trapped"
exit 0
}
while [ true ]; do
sleep 2
done
Run Code Online (Sandbox Code Playgroud)
当运行这个以后再做
kill -SIGINT (pid)
Run Code Online (Sandbox Code Playgroud)
一切都按预期工作,打印"被困"并退出.
现在,如果我从父脚本启动相同的脚本,如下所示:
#!/bin/bash
echo "starting the child"
./child.sh &
Run Code Online (Sandbox Code Playgroud)
然后孩子不再陷入信号......?
更改为使用SIGTERM而不是SIGINT后,它似乎正常工作......?
如果我在脚本或-c片段中处理进程,则后台进程会忽略SIGINT和SIGQUIT:
例:
$ alias ps='ps -o pid,ppid,pgrp,sid,stat,tty,ignored,blocked,caught,wchan,min_flt,pmem,args --forest'
$ sh -c 'sleep 1000 & sleep 1000 | sleep 1000' & \
sleep 0.01; ps |grep -v -e ps -e grep
PID PPID PGRP SID STAT TT IGNORED BLOCKED CAUGHT WCHAN MINFL %MEM COMMAND
6197 2143 6197 6197 Ss pts/28 0000000000380004 0000000000010000 000000004b817efb wait 10039 0.0 -bash
7593 6197 7593 6197 S pts/28 0000000000000000 0000000000000000 0000000000010002 wait 148 0.0 \_ sh -c sleep 1000 & sleep 1000 | sleep 1000
7595 …
Run Code Online (Sandbox Code Playgroud)