我正在编写一个脚本来读取系统日志,并且在守护进程时遇到了问题。它被随机挂起,我一生都无法弄清楚为什么。这是代码:
守护进程
daemon_context = daemon.DaemonContext(files_preserve=[fh.stream])
with daemon_context:
logger.debug("In Daemon")
main()
主要的
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
fh = logging.FileHandler("./debug.log")
logger.addHandler(fh)
logger.debug("First")
def main():
while True:
from schema import parsed, engine, session
logger.debug("Main Loop")
logger.debug(args)
if args.filename is None:
logger.debug("In If")
with open("/absolute/path/to/log.log") as f:
readlines = f.readlines()
logger.debug(readlines)
content = [x.split() for x in readlines]
logger.debug(content)
else:
logger.debug("In Else")
with open(args.filename) as f:
readlines = f.readlines()
logger.debug(readlines)
content = [x.split() for x in readlines]
logger.debug(content)
logger.debug("After Ifs")
conn = engine.connect()
logger.debug(conn)
rows = session.query(parsed).count()
for entry in range(rows, len(content)):
# Code inside this for loop is unimportant to the problem at hand
日志记录
我正在记录所有内容,以找出错误被捕获的位置。
当我运行它时,python3 test.py --filename access.log
我得到:
First
In Daemon
Main Loop
Namespace(filename='access.log')
In Else
这就是整个日志。它只是停止。
但是如果我运行python3 test.py
,没有文件名参数,那么我会得到这个日志:
First
In Daemon
Main Loop
Namespace(filename=None)
In If
[]
[]
After Ifs
<sqlalchemy.engine.base.Connection object at 0x7fe11c1da5f8>
然后它无限重复,直到我停止任务。
我只是不明白为什么它会停止。如果有人可以帮助我,那就太好了。在我尝试使用python-daemon
它之前,我有一个不同形式的这个脚本。所以我知道基本逻辑应该是合理的。