[英]Why may USR1 signals sent from background jobs in a Bash script not be reliably received by the parent shell process waiting for their completion?
[英]Sending kill -s USR1 from bash script stops the process while doing the same from terminal does not
有一个名为mimosa的nodejs脚本( https://github.com/dbashford/mimosa )
Node.js使用USR1将运行中的进程切换到调试模式
这是我手动执行的方法
$ cd myproj
$ mimosa watch -s # this runs node /path/to/mimosa watch -s
22:16:03 - Watching /Users/admin/Work/test-mimosa/assets
... # some more output
# check the pid from a different terminal
$ ps aux | grep mimosa
admin 79284 0.7 0.8 3153812 129272 s006 S+ 10:16PM 0:03.57 node /opt/local/bin/mimosa watch -s
# send debug signal from the 2nd terminal
kill -s USR1 79284
# nodejs output in the 1st terminal
Hit SIGUSR1 - starting debugger agent.
debugger listening on port 5858
如果我将mimosa作为后台进程运行,则它们的工作原理相同( mimosa watch -s &
)
现在,我需要自动化该过程:运行含羞草,获取其pid,发送USR1,等待用户的SIGTERM,杀死含羞草:
mimosa watch -s &
pid=$!
echo "mimosa pid: $pid"
trap "echo '\nSTOP'; kill $pid; exit" SIGHUP SIGINT SIGTERM
echo 'send debug'
kill -s USR1 $pid
wait $pid
该脚本将立即退出,含羞草进程也会退出(我再次使用grep对其进行了检查)。 控制台中的输出
$ ./debug.sh
mimosa pid: 79516
send debug
./debug.sh: line 11: 79516 User defined signal 1: 30 mimosa watch -s
怎么了,怎么解决?
发送调试信号时,含羞草可以向其自己的进程组发送信号吗? 那就可以解释了。
在交互式shell中,执行./program
使用其自己的进程组启动程序。 如果程序执行kill -s USR1 0
,它将永远不会退出该组。
在非交互式shell /脚本中,执行./program
会将其作为子项启动,但在同一进程组中。 如果孩子确实kill -s USR1 0
,它将杀死调用脚本。
如果是含羞草发送的信号,则可以在debug.sh
中trap 'echo ignoring' USR1 USR2
。
或者,尝试在启动含羞草之前使用set -m
打开作业控制。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.