I am trying to run some computational-intense program from Ruby via the following command:
%x(heavy_program)
However, I sometimes want to limit the running time of the program. So I tried doing
%x(ulimit -St #{max_time} & heavy_program)
But it seems to fail; the “&” trick does not work even when I try it in a running sh shell outside Ruby.
I’m sure there’s a better way of doing this…
use either
&∨:However using
ulimitmay be not what you really need, consider this code:ulimitlimits CPU time, andtimeoutlimits total running time, as we, humans, usually count it.so, for example, if you run
sleep 999999shell command withulimit -St 5– it will run not for 5 seconds, but for all 999999 becausesleepuses negligible amount of CPU time