Question

I try to limit the allowed of a sub process in a ksh script. I try using ulimit (hard or soft values) but the sub process always break the limit (if take longer than allowed time).

# 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
 )

result:

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

I expect a timeout error with a sys or user time of something like 0m2.00s

When i make a test directly from command line, the ulimit Hard seems to effectively limit the time bu not in script

System of test/dev is a AIX 6.1 but should also work other version and on sun and linux

Was it helpful?

Solution

Each process has its own time limits, but time shows the cumulative time for the script. Each time you create a child process, that child will have its own limits. So, for example, if you call cut and grep in the script then those processes use their own CPU time, the quota is not decremented from the script's, although the limits themselves are inherited.

If you want a time limit, you might wish to investigate trap ALRM.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top