Skip to content

Instantly share code, notes, and snippets.

@bfgits
Created August 15, 2017 06:29
Show Gist options
  • Save bfgits/f3ecdbb300e32414666a83302918a509 to your computer and use it in GitHub Desktop.
Save bfgits/f3ecdbb300e32414666a83302918a509 to your computer and use it in GitHub Desktop.
reading a log file continuously
#!/usr/bin/env python
#-*- coding: UTF-8 -*-
# Date: 7/26/17
# License Type: GNU GENERAL PUBLIC LICENSE, Version 3
import os, sys, time
name = "/var/log/shadowsocks.log"
current = open(name, "r")
curino = os.fstat(current.fileno()).st_ino
print(os.fstat(current.fileno()))
while True:
while True:
buf = current.read(1024)
if buf == "":
break
sys.stdout.write(buf)
try:
if os.stat(name).st_ino != curino:
print("curino has been changed@")
new = open(name, "r")
current.close()
current = new
curino = os.fstat(current.fileno()).st_ino
continue
except IOError:
pass
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment