简体   繁体   English

解决意外退出的python守护进程?

[英]Troubleshoot python daemon that quits unexpectedly?

What's the best way to monitor a python daemon to determine the cause of it quitting unexpectedly? 监视python守护程序以确定其意外退出的原因的最佳方法是什么? Is strace my best option or is there something Python specific that does the job? 是我最好的选择还是有特定的Python工作?

I would generally start by adding logging to it. 我通常会首先添加日志记录。 At a minimum, have whatever is launching it capture stdout/stderr so that any stack traces are saved. 至少,无论启动什么,它都会捕获stdout / stderr,以便保存任何堆栈跟踪。 Examine your except blocks to make sure you're not capturing exceptions silently. 检查您的except块以确保您没有以静默方式捕获异常。

You can use pdb : 你可以使用pdb

python -m pdb myscript.py

Running your program like this will cause it to enter post-mortem debugging if it exits abnormally. 像这样运行程序会导致它在异常退出时进入事后调试。 If you have an idea where the problem is you can also use import pdb; pdb.set_trace() 如果你知道问题所在,你也可以使用import pdb; pdb.set_trace() import pdb; pdb.set_trace() at the point you want to start debugging. import pdb; pdb.set_trace()在您要开始调试的位置。 Also logging profusely helps. 记录也很有帮助。

As the answer above, try to add Logging, however be carefull if you are using python-daemon module it will not work with Logging module when logging to a file, so you should make the logging manually to a file. 如上所示,尝试添加Logging,但是如果使用python-daemon模块则要小心,它在登录文件时不能与Logging模块一起使用,因此您应该手动将日志记录到文件中。

Also, make your daemon restarts after it has failed by running it inside a loop and catch exceptions inside the loop. 此外,通过在循环内运行守护程序并在循环内捕获异常,使守护程序在失败后重新启动。

Example: 例:

while True:
    try:
        log_to_file("starting daemon")
        run_daemon()
        log_to_file("daemon stopped unexpectedly")
        sleep(1)
    except Exception as err:
        log_to_file(err)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM