简体   繁体   English

从bash脚本发送kill -s USR1会停止该过程,而从终端执行此操作不会

[英]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.shtrap '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.

相关问题 为什么等待完成的父 shell 进程不能可靠地接收从 Bash 脚本中的后台作业发送的 USR1 信号? - 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? 在 USR1 信号后可靠地终止睡眠进程 - Reliably kill sleep process after USR1 signal 从bash脚本中杀死ssh或\\和远程进程 - Kill ssh or\and remote process from bash script 使用bash脚本杀死在另一个终端中运行的进程并关闭该终端 - Using bash script to kill a process running in another terminal and closing the terminal Bash脚本,通过从PID文件中提取杀死进程 - Bash Script, Kill process by pulling from PID file 如何在具有“for”和“R”代码的服务器中运行 bash 脚本,我可以退出终端并且不会终止进程? - How do I run a bash script in a server with `for` and `R` code that I can exit the terminal and does not kill the process? bash如何杀死父进程,或从bash模块脚本中的函数退出父进程 - bash how to kill parent process, or exit from parent process from a function in bash module script 在bash脚本中杀死进程 - kill a process in bash script 如何从终端杀死正在运行的bash函数? - How to kill a running bash function from terminal? 从父Bash进程杀死子进程 - Kill a child process from parent Bash process
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM