繁体   English   中英

无限循环中的Python脚本

[英]Python script in infinite loop

创建文件后脚本未退出。

如果我将以下status与1个字符串脚本一起使用可以正常工作,并且在install.txt创建后退出,但是在读取带有tail -f脚本的日志时,在install.txt文件创建后永远不会退出并继续读取日志。 甚至re.findall()也无法读取日志。 我不明白有什么区别,我做错了什么?

import sys
import os
import re 
import os.path


def getDownloadStatus():
    #status = "2019-09-16 8:52:05.924|main|INFO|CLIENT|Downloading Fileset: Adobe_Creative_Cloud_Desktop - TEST ID: 12273434579618"
    status = os.system('tail -f /var/log/fwcld.log | grep "Downloading Fileset:"')
    res = re.findall(r'\w+', status) 
    say = "Fileset"
    if say in res:
        split = res.index(say)  
        after = res[split+1:]
        x=' '.join(after)
    if(os.path.isfile('/var/log/install.txt')):
        return "Installations complete"
    else:
        return (x)

#Script
while not getDownloadStatus() == "Installations complete":
    newMES = getDownloadStatus()
    print(newMES)
    os.system('shutdown -r now')

首先,在Unix上os.system的结果是状态而不是您可以搜索的字符串。 只能在Windows上它看起来像它可能是从执行的命令的标准输出产生的字符串(即依赖于操作系统的是,你不能在这里使用的最佳工具红旗-看到subprocess模块)。

其次, -f标志指向tail的点是告诉它不要退出,而是(在这种情况下)保持管道打开,以等待写入/var/log/fwcld.log末尾的更多行。 因此,直到您杀死它,该tail过程才会终止。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM