1 |
sudo pip install pyinotify |
Example
1 |
a@a:~$ vim notify_test.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 |
import pyinotify class MyEventHandler(pyinotify.ProcessEvent): def process_IN_ACCESS(self, event): print "ACCESS event:", event.pathname def process_IN_ATTRIB(self, event): print "ATTRIB event:", event.pathname def process_IN_CLOSE_NOWRITE(self, event): print "CLOSE_NOWRITE event:", event.pathname def process_IN_CLOSE_WRITE(self, event): print "CLOSE_WRITE event:", event.pathname def process_IN_CREATE(self, event): print "CREATE event:", event.pathname def process_IN_DELETE(self, event): print "DELETE event:", event.pathname def process_IN_MODIFY(self, event): print "MODIFY event:", event.pathname def process_IN_OPEN(self, event): print "OPEN event:", event.pathname def main(): # watch manager wm = pyinotify.WatchManager() wm.add_watch('/var/log', pyinotify.ALL_EVENTS, rec=True) # event handler eh = MyEventHandler() # notifier notifier = pyinotify.Notifier(wm, eh) notifier.loop() if __name__ == '__main__': main() |
Test
1 |
python notify_test.py |
Results
1 2 3 4 5 6 7 8 |
MODIFY event: /var/log/auth.log MODIFY event: /var/log/syslog MODIFY event: /var/log/syslog MODIFY event: /var/log/auth.log MODIFY event: /var/log/auth.log MODIFY event: /var/log/syslog MODIFY event: /var/log/auth.log MODIFY event: /var/log/syslog |
References
1. http://www.saltycrane.com/blog/2010/04/monitoring-filesystem-python-and-pyinotify/