如何在脚本之间发送信号 SIGINT?
我想捕获从 Script-A.sh 发送到 Script-B.sh 的信号 所以在 Script-A.sh 中我使用命令:
(发送 SIGINT 到 Script-B.sh)
杀死 -2 $PID_Script-B.sh
在 Script-B.sh 中我捕获信号并调用函数 Clean
陷阱“干净”2
它不起作用,而是 Script-B.sh 立即被杀死而不执行 Clean !!
我还注意到,如果我想从终端发送 SIGINT 到捕获它的任何脚本,ctrl-c
将被正确捕获,但如果我通过命令 指定信号则不会kill -2 $pid_of_script
关于发送 SIGINT 的两种方法之间的区别(ctrl-c
VS kill -2 $pid_of_script
)以及如何实现的任何想法我可以将 SIGINT 从一个脚本发送到另一个脚本吗?
I want to trap a signal send from Script-A.sh to Script-B.sh
so in Script-A.sh i use the command:
(Send SIGINT to Script-B.sh)
kill -2 $PID_Script-B.sh
And in Script-B.sh i catch the signal and call function Clean
trap 'Clean' 2
It does not work, instead the Script-B.sh is killed right away without performing the Clean !!
What i notice also is that if i want to send SIGINT from terminal to any script that traps it, a ctrl-c
will be caught correctly, but not if i specify the signal via the command kill -2 $pid_of_script
Any idea about the difference between the two method to send the SIGINT (ctrl-c
VS kill -2 $pid_of_script
), and how i can send a SIGINT from a script to another?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我能够重现您报告的行为。我的假设是,由于脚本是从非交互式 shell(作为脚本的子级) 运行,因此
SIGINT
是一个键盘信号,被忽略。来自
info bash
:我发现,如果您使用
SIGUSR1
等其他信号来陷阱
和杀死
,它就会起作用。来自<的其他信息代码>人bash:
和
和
I was able to reproduce the behavior you report. My hypothesis is that since the script is running from a non-interactive shell (as a child of a script) that
SIGINT
, which is a keyboard signal, is ignored.From
info bash
:I have found that if you
trap
andkill
using another signal such asSIGUSR1
it works.Additional information from
man bash
:and
and
在脚本 A 中:陷阱函数如下所示,它将调用 scriptA.sh 中的 trap_mesg() 函数。 KILL 信号(2/中断,5/终止 - 默认)。您所要做的就是在从 scriptA.sh 调用 scriptB.sh 后获取正在运行的 scriptB.sh 进程/会话的 PID(nohup ... & 将为您提供或使用 ps 命令)
现在,在 scriptB.sh 中,执行相同/相似的操作,但仅针对 scriptB 陷阱作业(如调用 clean)。
这样,您就不必在 scriptA.sh 中将 scriptB.sh 作为“. scriptB.sh ....”进行源代码/调用。
In script A: Trap function will look like following which will call trap_mesg() function in scriptA.sh. KILL Signal (2/INTerrupt, 5/TERMinate-default). All, you have to do is to get the PID of a runing scriptB.sh process/session once scriptB.sh is called from scriptA.sh (nohup ... & will give you or use ps command)
Now, within scriptB.sh, do the same/similar but just for scriptB trap job (like calling clean).
This way, you dont have to source/call scriptB.sh within scriptA.sh as ". scriptB.sh ...."