-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtail-F_inotify.py
executable file
·79 lines (64 loc) · 2.43 KB
/
tail-F_inotify.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!python
### Use inotify to tail logfile (argv[1]), mimicking tail -F (follow if rotated).
###
### Usage: ./tail-F_inotify.py /path/to/file
###
### @author: "Charlie Schluting" <[email protected]>
### Copyright: the only free-as-in-freedom one: MIT license
###
import sys, os, pyinotify
from optparse import OptionParser
parser = OptionParser()
parser.add_option("--debug", help="print debug messages", action="store_true", dest="debug")
(options, args) = parser.parse_args()
myfile = args[0]
if options.debug:
print "I am totally opening " + myfile
wm = pyinotify.WatchManager()
# watched events on the directory, and parse $path for file_of_interest:
dirmask = pyinotify.IN_MODIFY | pyinotify.IN_DELETE | pyinotify.IN_MOVE_SELF | pyinotify.IN_CREATE
# open file, skip to end..
global fh
fh = open(myfile, 'r')
fh.seek(0,2)
# the event handlers:
class PTmp(pyinotify.ProcessEvent):
def process_IN_MODIFY(self, event):
if myfile not in os.path.join(event.path, event.name):
return
else:
print fh.readline().rstrip()
def process_IN_MOVE_SELF(self, event):
if options.debug:
print "The file moved! Continuing to read from that, until a new one is created.."
def process_IN_CREATE(self, event):
if myfile in os.path.join(event.path, event.name):
# yay, I exist, umm.. again!
global fh
fh.close
fh = open(myfile, 'r')
# catch up, in case lines were written during the time we were re-opening:
if options.debug:
print "My file was created! I'm now catching up with lines in the newly created file."
for line in fh.readlines():
print line.rstrip()
# then skip to the end, and wait for more IN_MODIFY events
fh.seek(0,2)
return
notifier = pyinotify.Notifier(wm, PTmp())
# watch the directory, so we can get IN_CREATE events and re-open the file when logrotate comes along.
# if you just watch the file, pyinotify errors when it moves, saying "can't track, can't trust it.. watch
# the directory".
index = myfile.rfind('/')
wm.add_watch(myfile[:index], dirmask)
while True:
try:
notifier.process_events()
if notifier.check_events():
notifier.read_events()
except KeyboardInterrupt:
break
# cleanup: stop the inotify, and close the file handle:
notifier.stop()
fh.close()
sys.exit(0)