首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从bash脚本发送kill -s USR1会停止该进程,而从终端发送相同的命令则不会

从bash脚本发送kill -s USR1会停止该进程,而从终端发送相同的命令则不会
EN

Stack Overflow用户
提问于 2013-04-29 03:23:48
回答 1查看 2K关注 0票数 1

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

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

下面是我手动操作的方法

代码语言:javascript
复制
$ 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:

代码语言:javascript
复制
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进行检查)。控制台中的输出

代码语言:javascript
复制
$ ./debug.sh
mimosa pid: 79516
send debug
./debug.sh: line 11: 79516 User defined signal 1: 30 mimosa watch -s

出什么问题了,怎么解决?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-20 21:15:05

当您发送调试信号时,mimosa是否会向自己的进程组发送信号?这就可以解释了。

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

在非交互式shells /脚本中,执行./program将作为子进程启动,但在同一进程组中。如果该子进程执行kill -s USR1 0,它将终止调用脚本。

您可以在debug.sh中执行trap 'echo ignoring' USR1 USR2,以防这些信号是由mimosa发送的。

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

另请参阅I have "trap 'echo ignore' USR1" in my called script, why does the calling script get killed?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16266808

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档