美文网首页
shell中定时器-指定时间后检查上条命令

shell中定时器-指定时间后检查上条命令

作者: sunland_0416 | 来源:发表于2021-03-04 08:27 被阅读0次

    工作中需要写个脚本,在一条命令执行后,过指定时间比如10秒后检查命令是否执行成功。
    通过搜索发现两个思路
    第一,
    参考https://www.cnblogs.com/JeffChen/archive/2011/12/26/2600241.html

    #!/bin/ksh
    #Delaytime=0
    AlarmHandler()
    {
        echo "Get SIGALAM"
        KillSubProcs
        exit 14
    }
    KillSubProcs()
    {
    #kill cbm
        echo "it's time out,kill cbm here"
        if [ $? -eq 0 ];then
            echo "Sub-processes killed."
        fi
    }
     
    SetTimer()
    {
        Delaytime=$1
        echo $Delaytime
        if [ $Delaytime  -ne 0 ];then
            sleep $Delaytime  && kill -s 14 $$ &
            #CHPROCIDS="$CHPROCIDS $!"
            TIMERPROC=$!
        fi
    }
    UnsetTimer()
    {
        echo "Start to unset timer"
        kill $TIMERPROC
    }
     
    trap AlarmHandler 14
    SetTimer 30
    #CHPROCIDS="$CHPROCIDS $!"
    #wait $!
    sleep 40
    UnsetTimer
    echo "ALL Done."
    exit 0
    

    第二,通过touch命令生成一个文件,然后检查文件是否存在

    func(){
        ls>>abc.txt
        touch finished
    }
    func &
    sleep 10
    if [ -f finished ]
    then
        echo "finished"
    else
        echo "not finished"
    fi
    rm -f finished
    

    相关文章

      网友评论

          本文标题:shell中定时器-指定时间后检查上条命令

          本文链接:https://www.haomeiwen.com/subject/qkuhqltx.html