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

前端之家收集整理的这篇文章主要介绍了如何从脚本发送SIGINT信号到脚本? BASH前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想捕获从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.

猜你在找的Bash相关文章