4

监视 python 守护程序以确定它意外退出的原因的最佳方法是什么?strace 是我最好的选择,还是有一些 Python 特定的东西可以完成这项工作?

4

3 回答 3

1

我通常会从向其中添加日志记录开始。至少,让启动它的任何内容捕获 stdout/stderr 以便保存任何堆栈跟踪。检查您的except块以确保您没有静默捕获异常。

于 2011-05-11T19:12:23.957 回答
0

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.

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)
于 2011-05-12T00:23:04.367 回答
0

您可以使用pdb

python -m pdb myscript.py

像这样运行你的程序会导致它在异常退出时进入事后调试。如果您知道问题出在哪里,也可以import pdb; pdb.set_trace()在要开始调试的地方使用。日志记录也有很大帮助。

于 2011-05-11T19:13:53.937 回答