方1:
import subprocess
if __name__ == '__main__':
log_file = '/tmp/debug.log'
cmd = 'tailf -1 {}'.format(log_file)
pp = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
while True:
line = pp.stdout.readline().strip()
row = line.decode()
print(row)
方2:
import time
if __name__ == '__main__':
file = open('/tmp/debug.log')
while True:
where = file.tell()
row = file.readline()
if not row:
time.sleep(1)
file.seek(where)
else:
print(row)
方3:
import time
def follow(file_o):
file_o.seek(0, 2)
while True:
row = file_o.readline()
if not row:
time.sleep(1)
continue
yield row
if __name__ == '__main__':
file = open('/tmp/debug.log')
log_line = follow(file)
for line in log_line:
print(line)
方4:
import pyinotify僅在linux平台,用來監測檔案系統的變化,依賴于linux核心的inotify功能,inotify是一個事件驅動的通知器,其通知接口從核心空間到使用者空間通過3個系統調用,pyinotify結合這些系統調用提供一個頂級的抽象和一個通用的方式來處理這些功能,pyinotify僅是對inotify的py封裝
if __name__ == '__main__':
file = '/tmp/debug.log'
wm = pyinitofy.WatchManager()對象儲存了需要監視的檔案和目錄,及監視檔案和目錄的哪些事件
wm.add_watch(file, pyinotify.ALL_EVENTS)
notifier = pyinotify.Notifier(wm)是pyinotify子產品最重要的類,用來讀取通知和處理事件,預設Notifier處理事件的方式是列印事件;Notifier類在初始化時接受多個參數,但僅WatchManager對象是必須傳的參數,Notifier類根據WatchManager對象中的配置來決定如何處理事件
notifier.loop()
<Event dir=False mask=0x20 maskname=IN_OPEN name='' path=/tmp/debug.log pathname=/tmp/debug.log wd=1 >
<Event dir=False mask=0x1 maskname=IN_ACCESS name='' path=/tmp/debug.log pathname=/tmp/debug.log wd=1 >
<Event dir=False mask=0x10 maskname=IN_CLOSE_NOWRITE name='' path=/tmp/debug.log pathname=/tmp/debug.log wd=1 >
<Event dir=False mask=0x20 maskname=IN_OPEN name='' path=/tmp/debug.log pathname=/tmp/debug.log wd=1 >
<Event dir=False mask=0x2 maskname=IN_MODIFY name='' path=/tmp/debug.log pathname=/tmp/debug.log wd=1 >
<Event dir=False mask=0x8 maskname=IN_CLOSE_WRITE name='' path=/tmp/debug.log pathname=/tmp/debug.log wd=1 >
inotify提供了多種事件:
IN_ACCESS;
IN_MODIFY;
IN_ATTRIB,中繼資料被修改;
IN_CLOSE_WRITE,一個打開且等待寫入的檔案或目錄被關閉;
IN_CLOSE_NOWRITE,一個以隻讀方式打開的檔案或目錄被關閉;
IN_OPEN;
IN_MOVED_FROM,被監控項目或目錄中的檔案被移除監控區域;
IN_MOVED_TO,檔案或目錄被移入監控區域;
IN_CREATE;
IN_DELETE;
IN_MOVE,檔案被移動,等同于IN_CLOSE_NOWRITE;
multi_event = pyinotify.IN_OPEN | pyinotify.IN_CLOSE_WRITE # 可通過|監控多個事件
例:
import pyinotify, time, os
if __name__ == '__main__':
fn = '/tmp/debug.log'
file = open(fn, 'r')
class ProcessTransientFile(pyinotify.ProcessEvent):
def process_IN_MODIFY(self, event):
row = file.readline()
if row:
print(row)
st_results = os.stat(fn)
print('---stat', st_results)
st_size = st_results[6]
file.seek(st_size)
wm = pyinotify.WatchManager()
wm.watch_transient_file(fn, pyinotify.IN_MODIFY, ProcessTransientFile)
notifier = pyinotify.Notifier(wm)
notifier.loop()
]$ stat debug.log
‘debug.log’
Size: 60 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 8477759 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2022-01-13 10:36:08.569134064 +0800
Modify: 2022-01-13 10:36:08.568134057 +0800
Change: 2022-01-13 10:36:08.568134057 +0800
Birth: -
]$ python3 test.py
---stat os.stat_result(st_mode=33188, st_ino=8477759, st_dev=64768, st_nlink=1, st_uid=0, st_gid=0, st_size=60, st_atime=1642041368, st_mtime=1642041368, st_ctime=1642041368)