Python 执行tail文件并操作

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
原文地址:https://www.cnblogs.com/cedrelaliu/p/11714017.html