欲陷阱从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将被正确地抓住,但如果我指定经由命令kill -2 $pid_of_script

的信号

关于两个方法来发送信号情报(ctrl-c VS kill -2 $pid_of_script),以及如何我可以从脚本发送信号情报到另一个之间的差值的任何想法?

有帮助吗?

解决方案

我能够重现您报告的行为。我的假设是因为脚本运行的 一个非交互的shell(作为脚本的孩子)是SIGINT,这是一个键盘的信号,将被忽略。

info bash

  

背景过程是那些来自其处理组ID不同          终端的;这样的方法是免疫的键盘产生的信号。

我发现,如果你trapkill使用其他信号,如SIGUSR1它的作品。

man bash其他信息:

  

非内置通过bash中运行的命令有设定为通过从它的父壳继承的值的信号处理程序。当作业控制是无效,异步命令忽略SIGINT和SIGQUIT除了这些继承处理程序。

  

如果bash中正在等待要完成的命令,并且接收针对其陷阱已经设定的信号时,陷阱不会直到命令完成执行。   

  

上任何SIGCHLD陷阱为每个子执行的退出。

其他提示

在脚本甲:陷阱函数看起来就像以下这将调用trap_mesg()函数在scriptA.sh。 KILL信号(2 /中断,5 /终止默认值)。所有的,你所要做的就是获得乳宁scriptB.sh进程/会话的PID一旦scriptB.sh从scriptA.sh称为(nohup的...&会给你或使用ps命令)

trap_mesg ()
{
 #...do something here for script B..
 # i.e. 
 kill -2 PID_of_ScriptB.sh_running_process_session
 sleep 10; #just in case, not reqd though.
 #show using ps -eAf|grep "scriptB" ... if null means, scriptB is gone. user is happy now.
 #...before actually exiting out...
 #show script A is exiting out as ScriptB is dead already, time for scriptA now.
 #...do something here..
}

#####################################
## Trap signals : INT, TERM. catch ##
#####################################
#Set NULL/Blank value to trap_call. This variable will help in running trap_mesg only once during the life of this script.
trap_call="";

trap 'if [ -z ${trap_call} ]; then trap_call="1"; trap_mesg ; fi' 2 15
##################################

结果,搜索结果 现在,内scriptB.sh,做相同/相似,但只是scriptB陷阱作业(比如调用干净)。 结果

clean ()
{
echo "karoge seva to milega meva"; 
rm -fr /some/folder_file
}

trap_mesg ()
{
 #...do something here JUST for script B trap message work..
 # i.e. 
 clean;
 #...do something here..
}

#####################################
## Trap signals : INT, TERM. catch ##
#####################################
#Set NULL/Blank value to trap_call. This variable will help in running trap_mesg only once during the life of this script.
trap_call="";

trap 'if [ -z ${trap_call} ]; then trap_call="1"; trap_mesg ; fi' 2 15
##################################

此方式,你不必源/内scriptA.sh调用scriptB.sh为 “scriptB.sh ....”

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top