免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2292 | 回复: 0
打印 上一主题 下一主题

Set Timeout For A Shell Command [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-09-09 14:15 |只看该作者 |倒序浏览
老外写的,觉得很不错
原帖 http://www.cyberciti.biz/faq/she ... d-under-alarmclock/
  1. :
  2. ##########################################################################
  3. # Shellscript:        timeout - set timeout for a command
  4. # Author     :        Heiner Steven <heiner.steven@odn.de>
  5. # Date       :        29.07.1999
  6. # Category   :        File Utilities
  7. # Requires   :
  8. # SCCS-Id.   :        @(#) timeout        1.3 03/03/18
  9. ##########################################################################
  10. # Description
  11. #    o        Runs a command, and terminates it (by sending a signal) after
  12. #        a specified time period
  13. #    o        This command first starts itself as a "watchdog" process in the
  14. #        background, and then runs the specified command.
  15. #        If the command did not terminate after the specified
  16. #        number of seconds, the "watchdog" process will terminate
  17. #        the command by sending a signal.
  18. #
  19. # Notes
  20. #    o        Uses the internal command line argument "-p" to specify the
  21. #        PID of the process to terminate after the timeout to the
  22. #        "watchdog" process.
  23. #    o        The "watchdog" process is invoked by the name "$0", so
  24. #        "$0" must be a valid path to the script.
  25. #    o        If this script runs in the environment of the login shell
  26. #        (i.e. it was invoked using ". timeout command...") it will
  27. #        terminate the login session.
  28. ##########################################################################

  29. PN=`basename "$0"`                        # Program name
  30. VER='1.3'

  31. TIMEOUT=5                                # Default [seconds]

  32. Usage () {
  33.     echo >&2 "$PN - set timeout for a command, $VER
  34. usage: $PN [-t timeout] command [argument ...]
  35.      -t: timeout (in seconds, default is $TIMEOUT)"
  36.     exit 1
  37. }

  38. Msg () {
  39.     for MsgLine
  40.     do echo "$PN: $MsgLine" >&2
  41.     done
  42. }

  43. Fatal () { Msg "$@"; exit 1; }

  44. while [ $# -gt 0 ]
  45. do
  46.     case "$1" in
  47.         -p)        ParentPID=$2; shift;;        # Used internally!
  48.         -t)        Timeout="$2"; shift;;
  49.         --)        shift; break;;
  50.         -h)        Usage;;
  51.         -*)        Usage;;
  52.         *)        break;;                        # First file name
  53.     esac
  54.     shift
  55. done

  56. : ${Timeout:=$TIMEOUT}                        # Set default [seconds]

  57. if [ -z "$ParentPID" ]
  58. then
  59.     # This is the first invokation of this script.
  60.     # Start "watchdog" process, and then run the command.
  61.     [ $# -lt 1 ] && Fatal "please specify a command to execute"
  62.     "$0" -p $ -t $Timeout &                # Start watchdog
  63.     #echo >&2 "DEBUG: process id is $"
  64.     exec "$@"                                # Run command
  65.     exit 2                                # NOT REACHED
  66. else
  67.     # We run in "watchdog" mode, $ParentPID contains the PID
  68.     # of the process we should terminate after $Timeout seconds.
  69.     [ $# -ne 0 ] && Fatal "please do not use -p option interactively"

  70.     #echo >&2 "DEBUG: $: parent PID to terminate is $ParentPID"

  71.     exec >/dev/null 0<&1 2>&1        # Suppress error messages
  72.     sleep $Timeout
  73.     kill $ParentPID &&                        # Give process time to terminate
  74.             (sleep 2; kill -1 $ParentPID) &&
  75.         (sleep 2; kill -9 $ParentPID)
  76.     exit 0
  77. fi
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP