我目前正在使用supervisor轻松地监控和守护一些Python脚本。然而,似乎supervisor无法正确记录日志。 我执行的脚本非常简单:
#!/usr/bin/env python
import pushybullet as pb
import sys, time, logging
# INIT LOGGING
logging.basicConfig(format='%(asctime)s @%(name)s [%(levelname)s]: %(message)s', level = logging.DEBUG)
if __name__ == '__main__':
try:
logging.info('Custom service started')
while True:
#here for the sake of example, actually doing real stuff here
time.sleep(2)
finally:
logging.info('Custom service stopped')
以下是相应的配置文件:
[program:myscript]
directory=/home/pi/Documents/Scripts_py
command=python -u myscript.py
autostart=true
autorestart=true
所以我基于对谷歌上的许多研究进行了许多测试。将日志行替换为打印并刷新stdout确实有效; 启动脚本时使用-u选项也是如此。但是,打印不符合我的需求,Python的日志记录模块很好用。因此,我尝试在每个日志行后刷新,并以无缓冲状态启动脚本,但是没有任何输出!
提前感谢任何帮助!