ulimit returns 0 as exit status... how to get 1 if process killed?
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 resour开发者_开发知识库ces 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.
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
精彩评论