我正在做一个带有覆盆子pi的小型服务器机房监控系统。几小时/几天后,检查温度和湿度是否在绿色范围内的python守护程序会停止。它每隔2秒在一个配置文件中加载一个while循环,其中定义温度和湿度间隔,然后检查当前值是否在范围内。 这是python守护进程的代码(checkSensorData.py):
import time
from tools import Tools
from sensorData import SensorData
from monlog import MonLog
from daemon import runner
CONF_FILE = "/home/pi/sys-conf/sys.conf"
DELAY = 2
class CheckSensorData():
def __init__(self):
self.stdin_path = "/dev/null"
self.stdout_path = "/dev/null"
self.stderr_path = "/dev/null"
self.pidfile_path = "/tmp/checkSensorData.pid"
self.pidfile_timeout = 5
self.temp_status = -1 # temperature status (0 => to low, 1 -> ok, 2 -> to high)
self.humd_status = -1 # humidity status (0 => to low, 1 -> ok, 2 -> to high)
def run(self):
self.monlog = MonLog()
self.sd = SensorData()
while True:
time.sleep(DELAY)
conf = Tools.loadConf(CONF_FILE)
self.check(conf)
def check(self, conf):
temperature = self.sd.getTemperature()
humidity = self.sd.getHumidity(temperature)
if temperature > float(conf["max_temperature"]) and self.temp_status!=2:
self.monlog.warning("Temperature is too high: "+str(temperature)+" °C")
self.temp_status = 2
elif temperature < float(conf["min_temperature"]) and self.temp_status!=0:
self.monlog.warning("Temperature is too low: "+str(temperature)+" °C")
self.temp_status = 0
elif temperature >= float(conf["min_temperature"]) and temperature <= float(conf["max_temperature"]) and self.temp_status!=1:
self.monlog.info("Temperature is ok: "+str(temperature)+" °C")
self.temp_status = 1
if humidity > float(conf["max_humidity"]) and self.humd_status!=2:
self.monlog.warning("Relative humidity is too high: "+str(humidity)+" %")
self.humd_status = 2
elif humidity < float(conf["min_humidity"]) and self.humd_status!=0:
self.monlog.warning("Relative humidity is too low: "+str(humidity)+" %")
self.humd_status = 0
elif humidity >= float(conf["min_humidity"]) and humidity <= float(conf["max_humidity"]) and self.humd_status!=1:
self.monlog.info("Relative humidity is ok: "+str(humidity)+" %")
self.humd_status = 1
checkSensorData = CheckSensorData()
daemonRunner = runner.DaemonRunner(checkSensorData)
daemonRunner.do_action()
以下是Tools类中加载配置文件的两种方法:
@staticmethod
def getLines(filename):
with open(filename, "r") as file:
return file.readlines()
@staticmethod
def loadConf(confFile):
lines = Tools.getLines(confFile)
conf = {}
for i in range(len(lines)):
line = lines[i].strip(" \n")
if line!="" and line[0]!="#":
data = line.split(":")
conf[data[0]] = data[1] # conf[key] = value
return conf
有没有人知道守护进程在几小时/几天后停止的原因?