杀死-2或杀死-INT:使我的运行脚本免受KILL信号2或-INT的影响

时间:2013-06-18 18:50:04

标签: int signals interrupt kill break

我有一个脚本:giga.sh(shell脚本BASH),它使用PUTTY会话运行。

我看到大多数人喜欢使用“Control + C”复制并粘贴为“Control + V”。很少有我的聪明的朋友使用鼠标右击然后复制,然后粘贴,我想知道,他们不知道Control + C?他们实际上是正确的,因为他们的经验:),就像giga.sh为PRODUCTION部署/任务运行时那样。

我想要的是:如果giga.sh正在运行并且有人打开了多个应用程序,并且他们定期执行复制/粘贴操作以从这些打开的应用程序中获取文本/图像的副本,那么如果鼠标控制意外发生PUTTY窗口/会话,然后执行“Control + C”不应该,杀死正在运行的giga.sh脚本。

现在我的脚本TRAPS在giga.sh内几个信号(2即INT,15,即TERM(默认))

这是否意味着,我所要做的就是从下面删除“2”(参见陷阱...行)并且它将使一个正在运行的giga.sh脚本免受按下意外控制+ c键的影响用户?我认为它不会因为我们不会捕获任何sig 2 / INT,所以想知道是否有人有更好的想法。新的trap / trap_mesg()分别处理sig 2和sig 15,会这样做!?

trap_mesg ()
{
 #...do something here..
 #...before actually exiting out...
 #...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
##################################

1 个答案:

答案 0 :(得分:0)

我认为它会是这样的:

trap_mesg_sig2 ()
{
 #...do something here..
 #...Don't exit and warn the user that if he really want to KILL -2/INT
 #...pass some Signal to script PID to continue as normal/resume instead of getting killed
 #   also reset trap_mesg_sig2 varialble back to "".
 #...maintain a counter i.e. if user presses Control+C (kill -2/INT) 3 times, only then 
 #   exit.
 #...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_sig2="";

trap 'if [ -z ${trap_call_sig2} ]; then trap_call_sig2="1"; trap_mesg_sig2 ; fi' 2
##################################



trap_mesg_sig15 ()
{
 #...do something here..
 #...before exiting out...
 #...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_sig15="";

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