zoukankan      html  css  js  c++  java
  • shell超时 输入超时-进程超时

    shell超时 输入超时-进程超时
    20121031 Chenxin

    1.使用多个shell进程的方式
    用主程序执行调用子进程1的输入,然后调用子进程2的时间要求,当时间到达后,子进程2就kill掉子进程1,达到时间限制的效果;

    2.使用read的-t参数
    cat t.sh

    !/bin/bash

    echo "input char,in 3 seconds ..."
    echo -n "[y/n] default [y]"
    read -t 3 n
    if [ -z $n ];then
    n=y
    fi

    ./t.sh
    input char,in 3 seconds ...
    [y/n] default [y]n
    Your input char is: n

    3.使用stty实现超时
    stty -icanon min 0 time 100  #set 10seconds,not 100seconds
    eval read varname  #=read $varname

    4.一般的超时(摘自《shell脚本专家指南》P91)
    cat t.sh

    !/bin/bash

    timeout()
    {
    waitfor= 2
    command=$*
    $command &
    commandpid=$!
    #echo "1 commandpid = $commandpid"

        (sleep $waitfor; kill -9 $commandpid > /dev/null 2>&1) &
        watchdogpid=$!
        #echo "2 watchdogpid = $watchdogpid"
    
        sleeppid=`ps $watchdogpid |awk '{ print $1}'`
        #echo "3 sleeppid = $sleeppid"
    
        wait $commandpid
        #echo "4 commandpid = $commandpid"
    
        kill $sleeppid > /dev/null 2>&1
        #echo "5 sleeppid = $sleeppid"
    

    }
    timeout ping 114.112.69.113 -c 5

    程序执行ping操作,正常是5s的ping,在第2s后,就会被kill掉;如果在kill前已经自动结束了的话,则在wait $commandpid后,kill掉sleeppid;

  • 相关阅读:
    ping与telnet的区别
    TCP连接的建立与关闭
    网络的7层协议
    oracle数据库中,分天查询数目
    求某个字符在字符串中的第5个位置
    高精度乘
    高精度加法
    二叉排序树(建树,先序,中序,后序遍历)
    求哈夫曼树的带权路径长度和
    HDU_1237_简单计算器
  • 原文地址:https://www.cnblogs.com/chanix/p/12738002.html
Copyright © 2011-2022 走看看