我有一个python守护进程,它可以每分钟计算一次文件的行数,计算一些数学运算并计算最后一分钟添加的行数。输出是一个带有计数和时间戳的基本csv,这样我就可以用另一个脚本来绘制它。一小时或两小时后退出Python守护进程
守护程序运行一段时间,“它似乎在一小时或两小时之间”,然后退出。没有迹象表明为什么。
基本守护模板来源于:This post
import os
import subprocess
import time
from daemon import runner
import datetime
inputfilename="test_data"
class App():
def __init__(self):
self.stdin_path = '/dev/null'
self.stdout_path = '/dev/tty'
self.stderr_path = '/dev/tty'
self.pidfile_path = '/tmp/counter.pid'
self.pidfile_timeout = 5
def run(self):
counter1 = 0
counter_log = 0
while True:
count = 0
output = open("tpm_counter.log", 'a')
FILEIN = open(inputfilename, 'rb')
while 1:
buffer = FILEIN.read(8192*1024)
if not buffer: break
count += buffer.count('\n')
FILEIN.close()
counter_log = (count - counter1) * 30
now = str(datetime.datetime.now())
counter1 = count
line = str(counter_log)
output.write(line)
output.write(", ")
output.write(now)
output.write("\n")
output.close()
# set the sleep time for repeated action here:
time.sleep(60)
app = App()
daemon_runner = runner.DaemonRunner(app)
daemon_runner.do_action()
没有人有任何想法,为什么这可能会退出?
你是如何启动它的?尝试将stdout和stderr重定向到一个文件,并查看它是否在存在之前记录了某些内容。例如,如果使用bash shell,该命令将是python d.py 2>&1> /tmp/my.log –
我以“python mydaemon.py start”开始它我已经提前并重定向它,我们将会看到下次退出时会发生什么。 – secumind