A sample implementation that monitors the logs that can be confirmed with
dmesg, such as
tail -F, for tail updates.
The ring buffer output by
dmesg can be opened as a file by
/ dev / kmsg or
/ proc / kmsg.
Since it blocks with
read on the last line, ʻO_NONBLOCK` is specified only when skipping is processed.
#!/usr/bin/env python2 # cofing: UTF-8 import os import fcntl import time def readLines(path): with open(path, "r") as fp: stat = os.stat(path) fp.seek(stat) where = None # ================= # seek to last line # ================= fd = fp.fileno() flag = fcntl.fcntl(fd, fcntl.F_GETFL) fcntl.fcntl(fd, fcntl.F_SETFL, flag | os.O_NONBLOCK) try: # FIXME for last in fp: pass except IOError as e: if e.errno != 11: raise fcntl.fcntl(fd, fcntl.F_SETFL, flag) # ================= # tailf impl # ================= try: while True: where = fp.tell() line = fp.readline() yield line except KeyboardInterrupt: pass if __name__ == "__main__": import sys for line in readLines("/dev/kmsg"): if str(line).find("stop tailf") >= 0: print "### stop watch tailf ###" break else: print line,
fp.readline ()when the last line is reached.
The execution and its result are as follows.
Execution result and end(Monitoring side)
$ ./tailf.py 12,642,9584294872,-;aaa 12,643,9588998703,-;bbb 12,644,9593362017,-;ccc ### stop watch tailf ###
By redirecting to
/ dev / kmsg, the output of the driver log is confirmed in a pseudo manner.
Execution result and end(Update side sample)
$ sudo sh -c "echo aaa > /dev/kmsg" $ sudo sh -c "echo bbb > /dev/kmsg" $ sudo sh -c "echo ccc > /dev/kmsg" $ sudo sh -c "echo stop tailf > /dev/kmsg"
I didn't know a good way to seek to the last line of the file in python, so I implemented it by reading everything up to the last line. In the case of a huge file, it may take a long time just to skip it. (When changing the size of the ring buffer, etc.)
# FIXME for last in fp: pass
Emerge Technology: Tail-like with Python Output the line containing the specified character string from the text file --Qiita python - What is the most efficient way to get first and last line of a text file? - Stack Overflow Get last n lines of a file with Python, similar to tail - Stack Overflow python Non-block read file - Stack Overflow