From: Alexander Schmidt Date: Tue, 6 Oct 2020 18:07:39 +0000 (+0200) Subject: refined file following X-Git-Url: http://git.treefish.org/~alex/logalert.git/commitdiff_plain/9bd4e7752ed07286a8e6b43f92c2820192539a5c?ds=inline;hp=e05944f24d3638efeb69411293ff4a3f17cdfb93 refined file following --- diff --git a/src/logalert.py b/src/logalert.py index 9722014..df9de07 100755 --- a/src/logalert.py +++ b/src/logalert.py @@ -10,16 +10,19 @@ import time MAX_LINES = 10 ALERT_INTERVAL = 86400 -def follow(filename): +def follow(path): while True: try: - with open(filename, "r") as f: + fd = os.open(path, os.O_RDONLY) + current_ino = os.fstat(fd).st_ino + with os.fdopen(fd, "r") as f: logging.info("Re-attached to log file.") for line in f: pass while True: line = f.readline() if not line: - if not os.path.exists(filename): + if os.stat(path).st_ino != current_ino or \ + os.stat(path).st_size < f.tell(): break else: time.sleep(1.0)