3

I have a badly coded infinite looping program that I want to run on the command line - but not forever. I want to use ulimit so if it loops forever, it gets cut off.

I'm trying:

$> bash -c "ulimit -t 1; java myinfloopprogram"

but it's like nothing is happening. What's going on, is my commandline command wrong? myinfloopprogram runs just fine.

I'm running a terminal and the version is Ubuntu 9.10 .

Scott Pack
  • 14,907
  • 10
  • 53
  • 83
rlb.usa
  • 163
  • 10

3 Answers3

6

Is the looping program actually using CPU?

-t is for CPU time, not wall clock time, so if your program is not actually using any CPU time it won't be killed.

James
  • 7,643
  • 2
  • 24
  • 33
  • This is EXACTLY what it was, about two minutes of running = 1 second of CPU time for this program. Thanks! – rlb.usa Jan 25 '10 at 23:28
1

Maybe try something like this instead:

java myinfloopprogram &
pid=$!
for i in $(seq 1 60); do
    kill -0 $pid >/dev/null || break
    sleep 1
done
kill -0 $pid >/dev/null || kill -TERM $pid
Peter Eisentraut
  • 3,665
  • 1
  • 24
  • 21
1

ulimit is probably not what you need. You need some sort of bash timeout feature. There's nothing built into bash, but there are a few scripts floating around to do this. eg https://stackoverflow.com/questions/687948/timeout-a-command-in-bash-without-unnecessary-delay

Amandasaurus
  • 31,471
  • 65
  • 192
  • 253