如何从脚本发出一个信号SIGINT脚本? BASH [英] How to send a signal SIGINT from script to script ? BASH
问题描述
我要捕获的信号从Script-A.sh发送到Script-B.sh
所以在Script-A.sh我用命令
(SIGINT发送到Script-B.sh)结果
杀-2 $ PID_Script-B.sh
块引用>而在Script-B.sh我捕捉信号和通话功能的清洁
陷阱'干净'2
块引用>这是不行的,而不是Script-B.sh被立即杀死,而不执行干净!
我也注意到的是,如果我想从终端发送SIGINT到捕获它的任何脚本,一个
CTRL-C
将正确逮住,但如果我通过命令指定信号杀-2 $ pid_of_script
有关这两个方法之间的差异发送SIGINT任何想法(
CTRL-C
VS杀-2 $ pid_of_script
),以及如何从一个脚本发送SIGINT到另一个?问候,
调试
解决方案我能够重现您报告的行为。我的假设是因为脚本运行的的从的一个非交互的shell(为脚本的孩子)的
SIGINT
,这是一个键盘信号,将被忽略。从
信息庆典
:
后台进程是那些进程组ID从不同
终端的;这样的过程是免疫的键盘产生的信号。
块引用>我发现,如果你
陷阱
和杀
使用另一种信号,如SIGUSR1
它的工作原理。从
男人庆典
信息
被Bash运行非内置命令必须设置为从父外壳继承的值信号处理程序。当作业控制不生效,异步命令忽略SIGINT和SIGQUIT除了这些继承处理。
块引用>和
如果bash等待命令完成并接收其陷阱已设置的信号,陷阱将不会被直到命令完成执行。
块引用>和
在SIGCHLD任何陷阱为每个孩子退出执行。
块引用>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.shAnd 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 catched correctly, but not if i specify the signal via the commandkill -2 $pid_of_script
Any idea about the difference between the two method to send the SIGINT (
ctrl-c
VSkill -2 $pid_of_script
), and how i can send a SIGINT from a script to another ?Regards,
Debugger
解决方案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
:Background processes are those whose process group ID differs from the terminal's; such processes are immune to keyboard-generated signals.
I have found that if you
trap
andkill
using another signal such asSIGUSR1
it works.Additional information from
man bash
:Non-builtin commands run by bash have signal handlers set to the values inherited by the shell from its parent. When job control is not in effect, asynchronous commands ignore SIGINT and SIGQUIT in addition to these inherited handlers.
and
If bash is waiting for a command to complete and receives a signal for which a trap has been set, the trap will not be executed until the command completes.
and
Any trap on SIGCHLD is executed for each child that exits.
这篇关于如何从脚本发出一个信号SIGINT脚本? BASH的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!