how to handle os.system sigkill signal inside python?

走远了吗. 提交于 2019-12-10 02:56:34

问题


I have a python script where I call a lengthy process from the operating system. After a long while, the process that I call gets terminated by the system by SIGKILL signal.

Is it possible to handle this from inside Python like in a try and catch situation?

What method should I use to solve this issue. It is very important that this process keeps on running as long as possible without any interruptions.


回答1:


There is no way to handle SIGKILL

The signals SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.

If you're looking to handle system shutdown gracefully, you should be handling SIGTERM or a startup/shutdown script such as an upstart job or a init.d script.




回答2:


The answer is NO, you can't handle SIGKILL or kill -9 <pid> in any process. Only SIGHUP can be handled by shutdownhooks but not SIGKILL or -9. SIGKILL is made for aggressive killing the task and only works on kernel level; your process is unaware about the killing.

For keeping long running process you should write a small monitor program which keeps on checking whether the process is alive; if it is found to be killed, just restart the process.

OR

Run your process in below loop , hope you got it ;)

while true
   do
    /your/script
   sleep 1
done


来源:https://stackoverflow.com/questions/33242630/how-to-handle-os-system-sigkill-signal-inside-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!