本文介绍了python sigkill捕获策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有任何方法可以从OOM杀手那里捕获Sigkill.我有一个任务队列,而且经常创建一个庞大的任务,该任务被OOM杀死.这个:

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!

不起作用,因为无法捕获SIGKILL.所以........ SIGKILL之后有什么策略可以清理吗?我可以分叉,并观察子进程吗?如果是这样,父进程必须事先知道子进程打开的任何资源?或者我可以做一些版本的

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...)

当前,如果我在杀死OOM之后没有清理,我会留下很多子进程和其他事情,这些事情只会在重试任务时使情况变得更糟,并且很快,服务器将无法访问.

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>

要测试这种确切情况?

非常感谢!

推荐答案

SIGKILL本质上不能被捕获.

SIGKILL by its very nature cannot be trapped.

请参见 http://en.wikipedia.org/wiki/Unix_signal#SIGKILL :

SIGKILL信号被发送到进程以使其终止 立即(杀死).与SIGTERM和SIGINT相比,此信号不能 被捕获或忽略,并且接收过程无法执行任何操作 收到该信号后进行清理.

The SIGKILL signal is sent to a process to cause it to terminate immediately (kill). In contrast to SIGTERM and SIGINT, this signal cannot be caught or ignored, and the receiving process cannot perform any clean-up upon receiving this signal.

最好的做法是下次启动进程时,查找需要清理的所有内容.

The best thing to do is the next time your process launches, look for anything that needs to be cleaned up.

是的,kill -9 <pid>将向该进程发送一个SIGKILL. (确切地说,它发送第9个信号-恰巧SIGKILL在几乎每个系统上都具有数字9.您也可以编写kill -KILL <pid>,它使您可以通过名称而不是通过便携式方式通过数字来指定信号)

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 write kill -KILL <pid>, which lets you specify the signal by name instead of by number in a portable way.)

这篇关于python sigkill捕获策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-29 05:38