【问题标题】:How to limit allowed time for a script run as a sub process in ksh script如何限制脚本在 ksh 脚本中作为子进程运行的允许时间
【发布时间】:2013-11-06 22:46:04
【问题描述】:

我尝试在 ksh 脚本中限制允许的子进程。我尝试使用 ulimit(硬值或软值),但子进程总是打破限制(如果花费的时间超过允许的时间)。

# value for a test
Sc_Timeout=2
Sc_FileOutRun=MyScript.log.Running
Sc_Cmd=./AScriptToRunInSubShell.sh

(
 ulimit -Ht ${Sc_Timeout}
 ulimit -St ${Sc_Timeout}

 time (
  ${Sc_Cmd} >> ${Sc_FileOutRun} 2>&1
  )  >> ${Sc_FileOutRun} 2>&1

  # some other command not relevant for this
 )

结果:

1> ./MyScript.log.Running
ulimit -Ht 2
ulimit -St 2
1>> ./MyScript.log.Running 2>& 1

real    0m11.45s
user    0m3.33s
sys     0m4.12s

我预计系统或用户时间会出现超时错误,例如 0m2.00s

当我直接从命令行进行测试时,ulimit Hard 似乎有效地限制了时间但不在脚本中

test/dev 系统是 AIX 6.1,但也应该可以在其他版本以及 sun 和 linux 上运行

【问题讨论】:

    标签: ksh ulimit


    【解决方案1】:

    每个进程都有自己的时间限制,但time 显示脚本的累积时间。每次创建子进程时,该子进程都会有自己的限制。因此,例如,如果您在脚本中调用 cutgrep,那么这些进程会使用它们自己的 CPU 时间,尽管限制本身是继承的,但配额不会从脚本中减少。

    如果你想要一个时间限制,你可能希望调查trap ALRM

    【讨论】:

    • 坦克信息。我在想下一个过程允许的时间(所以使用 subshel​​l)。我用睡眠/杀死看门狗捕获进程 ID(在后台启动)替换 ulimit
    猜你喜欢
    • 2010-10-06
    • 1970-01-01
    • 2017-03-26
    • 2020-06-19
    • 1970-01-01
    • 2013-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多