1

I'm writing a shell script that called some programs that can use up all resources and effectively kill a machine. I must prevent this some happening.

My idea was to use ulimit to set resource limits (first time I've ever actually needed to use ulimit in practice) but I was a little surprised that the exit status of a killed process is 0.

How can my shell scripts limit resources AND detect a process being killed by the shell because it exceeded that limit?

I'm using bash but any comments would be worth reading.

SO Stinks
  • 3,258
  • 4
  • 32
  • 37
  • Just so we have the full picture, please post the exact invocation you are using in the bash script for `ulimit` AND the output of `ulimit -a`. Also, are the offending programs custom code that you control, or 3rd party packages? Also, I would recommend adding a tag for bash, that will get your problem a lot more eyes. Good luck. – shellter Jul 21 '11 at 12:42

1 Answers1

1

A program exceeding one of the ulimits will either terminate due to an error (e.g. out of file descriptors, processes) or eventually catch a signal (memory, CPU time). This means you simply test the exit status of the program in question.

ulimit ...
program
xs=$?
case $xs in
 (0) ;; # all fine
 (*) if [ $xs -gt 127 ]; then
        echo caught a signal...
     else
        echo some error...
     fi
esac
Jens
  • 69,818
  • 15
  • 125
  • 179