我有一个 python 守护程序,用于每分钟计算一次文件的行数,做一些数学运算并计算最后一分钟添加的行数。输出是一个带有计数和时间戳的基本 csv,以便我以后可以用另一个脚本绘制它。
守护进程运行了一段时间,“它似乎介于一两个小时之间”,然后退出。没有迹象表明为什么。
基本的守护进程模板来源于:这篇文章
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()
有谁知道为什么这可能会退出?