[英]Sending kill -s USR1 from bash script stops the process while doing the same from terminal does not
There's a nodejs script called mimosa ( https://github.com/dbashford/mimosa ) 有一个名为mimosa的nodejs脚本( https://github.com/dbashford/mimosa )
Nodejs uses USR1 to switch the running process to debug mode Node.js使用USR1将运行中的进程切换到调试模式
Here's how I do it manually 这是我手动执行的方法
$ 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
The same works if I run mimosa as a background process ( mimosa watch -s &
) 如果我将mimosa作为后台进程运行,则它们的工作原理相同( mimosa watch -s &
)
Now I need to automate the process: run mimosa, get its pid, send USR1, wait for user's SIGTERM, kill mimosa: 现在,我需要自动化该过程:运行含羞草,获取其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
This script exits immediately, so does the mimosa process (I check it with grep again). 该脚本将立即退出,含羞草进程也会退出(我再次使用grep对其进行了检查)。 The output in the console 控制台中的输出
$ ./debug.sh
mimosa pid: 79516
send debug
./debug.sh: line 11: 79516 User defined signal 1: 30 mimosa watch -s
What's wrong, how to fix? 怎么了,怎么解决?
Could mimosa be sending a signal to its own process group when you send the debug signal? 发送调试信号时,含羞草可以向其自己的进程组发送信号吗? That would explain it. 那就可以解释了。
In interactive shells, doing ./program
starts program with its own process group. 在交互式shell中,执行./program
使用其自己的进程组启动程序。 If program does something like kill -s USR1 0
, it'll never exit that group. 如果程序执行kill -s USR1 0
,它将永远不会退出该组。
In non-interactive shells / scripts, doing ./program
will start it as a child but in the same process group. 在非交互式shell /脚本中,执行./program
会将其作为子项启动,但在同一进程组中。 If the child does kill -s USR1 0
, it'll kill the calling script. 如果孩子确实kill -s USR1 0
,它将杀死调用脚本。
You could do trap 'echo ignoring' USR1 USR2
in your debug.sh
in case those are the signals being sent by mimosa. 如果是含羞草发送的信号,则可以在debug.sh
中trap 'echo ignoring' USR1 USR2
。
Alternatively, try turning on job control with set -m
before starting mimosa. 或者,尝试在启动含羞草之前使用set -m
打开作业控制。
See also I have "trap 'echo ignore' USR1" in my called script, why does the calling script get killed? 另请参阅我的被叫脚本中有“陷阱'echoignore'USR1”,为什么叫死的脚本被杀死?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.