代码之家  ›  专栏  ›  技术社区  ›  Tsf

有时限的Scons命令

  •  3
  • Tsf  · 技术社区  · 17 年前

    我想限制我在Linux下运行的程序的执行时间。我在我的scons脚本中添加了一行,如下所示:

    Command("com​","",​"ulimit -t 1; myprogram")
    

    并用无限循环程序对其进行了测试:它不起作用,程序永远运行。

    我错过了什么吗?

    2 回复  |  直到 6 年前
        1
  •  4
  •   richq luc    16 年前

    ulimit -t 1 意味着该限制被设置为1秒的CPU时间。如果你的无限循环程序使用任何类型的 sleep

    ulimit -t 1; ./myprogram
    

    bash: ulimit: cpu time: cannot modify limit: Operation not permitted
    

    Edit:看起来 the -t option is broken on Ubuntu 9.04 。修复已于2009年6月5日提交,但可能需要一段时间才能渗透到更新中——可能要到9.10才能修复。

    从历史上看,这个问题在Ubuntu 10.04中已经不存在了。

        2
  •  0
  •   Rob Audenaerde    15 年前

    (取自 http://newsgroups.derkeiler.com/Archive/Comp/comp.sys.mac.system/2005-12/msg00247.html )

    #!/bin/sh
    # timeout script
    #
    usage()
    {
    echo "usage: timeout seconds command args ..."
    exit 1
    }
    
    [[ $# -lt 2 ]] && usage
    seconds=$1; shift
    
    timeout()
    {
    sleep $seconds
    kill -9 $pid >/dev/null 2>/dev/null
    }
    
    eval "$@" &
    pid=$!
    timeout &
    wait $pid
    .