码迷,mamicode.com
首页 > 编程语言 > 详细

Python 执行tail文件并操作

时间:2019-10-21 16:09:49      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:col   self   pipe   pip   sele   python   sig   ret   imp   

技术图片
def log_search(self, logfile, search_content, timeout=10):
        import time
        import subprocess
        import select
        import signal
        import os

        f = subprocess.Popen([tail, -F, logfile], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        p = select.poll()
        p.register(f.stdout)

        try:
            while timeout>0:
                if p.poll(500):   # millisecond timeout
                    while True:
                        line = f.stdout.readline()
                        if line:
                            if search_content in line:
                                return True
                        else:  # no content read out
                            break
                time.sleep(1)
                timeout -= 1
            else:
                return False
        finally:
            print(unregister)
            p.unregister(f.stdout)
            print(kill pid)
            os.kill(f.pid, signal.SIGKILL)
View Code

 

Python 执行tail文件并操作

标签:col   self   pipe   pip   sele   python   sig   ret   imp   

原文地址:https://www.cnblogs.com/cedrelaliu/p/11714017.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!