【问题标题】:Kill -2 or Kill -INT : Make my running script immune to KILL signal 2 or -INTKill -2 或 Kill -INT :使我的运行脚本不受 KILL 信号 2 或 -INT 的影响
【发布时间】:2013-06-18 18:50:03
【问题描述】:

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

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

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

现在我的脚本在 giga.sh 中捕获了几个信号(2 即 INT,15 即 TERM(目前默认))。

这是否意味着,我所要做的就是从以下内容中删除“2”(请参阅​​陷阱...行),它将使正在运行的 giga.sh 脚本免受意外按 control+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
##################################

【问题讨论】:

    标签: int signals interrupt kill break


    【解决方案1】:

    我认为它会是这样的:

    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
    ##################################
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-18
      • 2011-09-25
      • 1970-01-01
      • 1970-01-01
      • 2014-01-02
      • 2011-12-13
      • 2019-01-10
      • 2021-04-28
      相关资源
      最近更新 更多