从bash脚本发送kill -s USR1会停止进程,而从terminal执行相同操作则不会

有一个名为mimosa的nodejs脚本( https://github.com/dbashford/mimosa )

Nodejs使用USR1将正在运行的进程切换到debugging模式

这是我如何手动

$ 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 watch -s & ),也是一样的作品

现在我需要自动化过程:运行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 

这个脚本立即退出,含羞草过程也一样(我再次用grep检查它)。 控制台中的输出

 $ ./debug.sh mimosa pid: 79516 send debug ./debug.sh: line 11: 79516 User defined signal 1: 30 mimosa watch -s 

怎么了,怎么解决?

当你发送debugging信号时,含羞草可以发送一个信号给它自己的进程组吗? 这将解释它。

在交互式shell中,. ./program program使用自己的进程组启动程序。 如果程序做了类似于kill -s USR1 0 ,它将永远不会退出该组。

在非交互的shell /脚本中,执行./program将作为一个subprocess启动,但在同一个进程组中。 如果孩子kill -s USR1 0 ,它会杀死调用脚本。

你可以在你的debug.sh中设置trap 'echo ignoring' USR1 USR2debug.sh ,以防含羞草发送信号。

或者,在启动含羞草之前,尝试使用set -m开启作业控制。

另请参见我有“陷阱”回声忽略“USR1”在我的被调用的脚本,为什么调用脚本被杀害?