加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何从脚本发送SIGINT信号到脚本? BASH

发布时间:2020-12-16 01:22:13 所属栏目:安全 来源:网络整理
导读:我想捕获从ScriptA.sh发送到Script-B.sh的信号 所以在Script-A.sh我使用命令 (Send SIGINT to Script-B.sh) kill -2 $PID_Script-B.sh 而在Script-B.sh中,我捕获信号并调用函数Clean trap ‘Clean’ 2 它不工作,而是Script-B.sh立即被杀死,而不执行清洁! 我
我想捕获从ScriptA.sh发送到Script-B.sh的信号
所以在Script-A.sh我使用命令

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

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

trap ‘Clean’ 2

它不工作,而是Script-B.sh立即被杀死,而不执行清洁!

我也注意到,如果我想从终端发送SIGINT到陷阱的任何脚本,ctrl-c将被捕获正确,但是如果我通过命令kill -2 $pid_of_script指定信号,

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

问候,

调试器

我能够重现您报告的行为.我的假设是,由于脚本是从非交互式 shell(作为脚本的子代)运行的,因此SIGINT(键盘信号)被忽略.

从信息bash:

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

我发现如果你使用另一个信号(如SIGUSR1)来捕捉和杀死它,它可以工作.

来自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.

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.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读