如何找到Python守护进程死亡的原因?
我使用 python-daemon 库在 python 中实现了一个守护进程。
然而,守护进程似乎会定期死亡(或被杀死),周期从一天到几个月不等。
我试图通过捕获异常、将它们记录到文件中并将它们邮寄给我来找到守护进程死亡的原因。我的脚本的守护进程部分大致如下:
import daemon
context = daemon.DaemonContext(
working_directory='/foo/',
pidfile=lockfile.FileLock('/foo/foo.pid')
)
try:
with context:
do_stuff()
except Exception, e:
log_exception_to_file(e)
mail_exeption_to_me(e)
我记录了相当多的异常并将其邮寄给我,所以我知道代码通常可以工作。
在大多数情况下,我什么也得不到,并且看门狗脚本会提醒我守护进程不再运行。有什么方法可以找出或跟踪守护进程死亡或被杀死的原因吗?
I've got a daemon implemented in python using the python-daemon library.
The daemon appears to periodically die (or is killed) however, where periodically varies from one day to several months.
I've tried to find the reason for the daemon dying by catching exceptions, logging them to a file, and mailing them to me. The daemon part of my script looks roughly like:
import daemon
context = daemon.DaemonContext(
working_directory='/foo/',
pidfile=lockfile.FileLock('/foo/foo.pid')
)
try:
with context:
do_stuff()
except Exception, e:
log_exception_to_file(e)
mail_exeption_to_me(e)
I've had quite a few exceptions logged and mailed to me, so I know the code generally works.
For the majority of cases, I get nothing, and a watchdog script alerts me to the fact that the daemon is no longer running. Is there some way I can find out or track why the daemon is either dying or being killed?
当您邮寄东西时,请检查内存消耗情况。也许它存在内存泄漏,并在耗尽所有可用 RAM 时死亡。
resource
模块会让应用程序找出它的 maxrss、ixrss、idrss 和 isrss,如果你每 5 分钟左右记录一次,应该很明显是否发生内存泄漏。While you are mailing stuff, check the memory consumption. Maybe it has a memory leak and dies when it consumes all available RAM. The
resource
module will let the app find out its maxrss, ixrss, idrss, and isrss and if you log those once every 5 minutes or so, it should be obvious whether or not there is a memory leak happening.