有一个名为 mimosa 的 Nodejs 脚本 ( https://github.com/dbashford/mimosa )
Nodejs使用USR1将运行进程切换到 Debug模式
这是我手动执行的方法
$ 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 &
),效果相同
现在我需要自动化该过程:运行mimosa,获取其pid,发送USR1,等待用户的SIGTERM,杀死mimosa:
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
这个脚本立即退出,mimosa 进程也立即退出(我再次用 grep 检查它)。 控制台输出
$ ./debug.sh
mimosa pid: 79516
send debug
./debug.sh: line 11: 79516 User defined signal 1: 30 mimosa watch -s
出了什么问题,如何修复?
最佳答案
当您发送调试信号时,mimosa 是否会向其自己的进程组发送信号?这样就可以解释了。
在交互式 shell 中,执行 ./program
会使用自己的进程组启动程序。如果程序执行类似 kill -s USR1 0
的操作,它永远不会退出该组。
在非交互式 shell/脚本中,执行 ./program
会将其作为子进程启动,但位于同一进程组中。如果子进程执行 kill -s USR1 0
,它将终止调用脚本。
您可以在 debug.sh
中执行trap 'echo ignoring' USR1 USR2
,以防这些是含羞草发送的信号。
或者,在启动 mimosa 之前尝试使用 set -m
打开作业控制。
另请参阅I have "trap 'echo ignore' USR1" in my called script, why does the calling script get killed?
关于bash - 从 bash 脚本发送kill -s USR1 会停止进程,而从终端执行相同操作不会停止进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16266808/