I was wondering if there was any way to catch the sigkill from the OOM killer. I have a task queue, and every so often a mammoth task is created that is killed by OOM. This:
catch Exception as ex:
# clean up!
does not work, as SIGKILL can't be caught. So........is there ANY strategy to clean up after a SIGKILL? Can I fork, and watch the child process? If so, any resources opened by the child process would have to be known in advance by the parent? Or could I just do some version of
ps -ef | grep <child pid> | xargs kill -9 (you get the idea...)
Currently, if I don't clean up after an OOM kill, I leave behind plenty of child processes and other things that just make it worse when the task is retried, and soon enough, the server is unreachable.
Finally, is it enough to just do:
kill -9 <process id>
to test this exact situation?
Thanks very much!
SIGKILL by its very nature cannot be trapped.
See http://en.wikipedia.org/wiki/Unix_signal#SIGKILL:
The best thing to do is the next time your process launches, look for anything that needs to be cleaned up.
And yes,
kill -9 <pid>
will send a SIGKILL to the process. (To be precise, it sends the 9th signal - it just happens that SIGKILL has the number 9 on pretty much every system. You could alternatively writekill -KILL <pid>
, which lets you specify the signal by name instead of by number in a portable way.)