如何在脚本之间发送信号SIGINT? [英] How to send a signal SIGINT from script to script?

查看:332
本文介绍了如何在脚本之间发送信号SIGINT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想捕获从Script-A.sh发送到Script-B.sh的信号 所以在Script-A.sh中,我使用以下命令:

I want to trap a signal send from Script-A.sh to Script-B.sh so in Script-A.sh i use the command:

(将SIGINT发送到Script-B.sh)
杀死-2 $ PID_Script-B.sh

(Send SIGINT to Script-B.sh)
kill -2 $PID_Script-B.sh

在Script-B.sh中,我捕获了信号并调用了Clean函数

And in Script-B.sh i catch the signal and call function Clean

捕获清除" 2

trap 'Clean' 2

它不起作用,而是立即执行Script-B.sh而不执行Clean !!

It does not work, instead the Script-B.sh is killed right away without performing the Clean !!

我还注意到,如果我想将SIGINT从终端发送到任何捕获该脚本的脚本,则会正确捕获ctrl-c,但是如果我通过命令kill -2 $pid_of_script

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

关于发送SIGINT的两种方法(ctrl-c VS kill -2 $pid_of_script)之间的区别的任何想法,以及如何将SIGINT从脚本发送到另一个脚本?

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?

推荐答案

我能够重现您报告的行为.我的假设是,由于脚本从 运行 ,因此,作为键盘信号的SIGINT会被忽略.

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.

来自info bash:

后台进程是那些进程组ID与 终端的;这样的过程不受键盘产生的信号的影响.

Background processes are those whose process group ID differs from the terminal's; such processes are immune to keyboard-generated signals.

我发现,如果您trapkill使用诸如SIGUSR1的其他信号,则它可以工作.

I have found that if you trap and kill using another signal such as SIGUSR1 it works.

来自man bash的其他信息:

由bash运行的非内置命令将信号处理程序设置为shell从其父级继承的值.当作业控制无效时,除了这些继承的处理程序之外,异步命令还会忽略SIGINT和SIGQUIT.

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.

如果bash正在等待命令完成并接收到已为其设置了陷阱的信号,则在命令完成之前将不执行陷阱.

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.

每个退出的孩子都会执行SIGCHLD上的任何陷阱.

Any trap on SIGCHLD is executed for each child that exits.

这篇关于如何在脚本之间发送信号SIGINT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆