#!/usr/bin/env python """ filemonitoring for a given filedirectory """ import os import sys try: import platform except ImportError, e: print e[0] sys.exit() try: from subprocess import Popen, PIPE except ImportError, e: print e[0] print "Please" sys.exit() if not os.path.isdir('/proc/sys/fs/inotify'): print "Please enable in the kernelconfiguration inotify." print "grep \"INOTIFY\" /boot/config-`uname -r`" sys.exit() try: from pyinotify import WatchManager, EventsCodes, ProcessEvent, Notifier except ImportError, e: print e[0] print "Please install pyinotify." print "For Debian do as root user 'apt-get install python-pyinotify'." print "For Gentoo do as root user 'emerge -av pyinotify'." print "For other distributions use your package manager." print "Or download it from website 'http://trac.dbzteam.org/pyinotify'." sys.exit() dir_to_monitoring = "/var/run" events_codes = EventsCodes.ALL_EVENTS # initial WatchManager wm = WatchManager() # override build in methodes class ProcessAction(ProcessEvent): def process_IN_CREATE(self, event): print "event: %s %s" % (event.event_name, os.path.join(event.path, event.name)) def process_IN_DELETE(self, event): print "event: %s %s" % (event.event_name, os.path.join(event.path, event.name)) def process_IN_OPEN(self, event): print "event: %s %s" % (event.event_name, os.path.join(event.path, event.name)) def process_IN_MODIFY(self, event): print "evnet: %s, %s" % (event.event_name, os.path.join(event.path, event.name)) def process_IN_CLOSE_WRITE(self, event): print "event: %s, %s" % (event.event_name, os.path.join(event.path, event.name)) def process_IN_ACCESS(self, event): print "event: %s, %s" % (event.event_name, os.path.join(event.path, event.name)) def process_IN_CLOSE_NOWRITE(self, event): print "event: %s, %s" % (event.event_name, os.path.join(event.path, event.name)) # initial Notifier notifier = Notifier(wm, ProcessAction()) # add the directory to WatchManager wm.add_watch(dir_to_monitoring, events_codes) # run it in loop till keyboar str + ctl is pressing while True: try: # process the queue of events as explained above notifier.process_events() if notifier.check_events(): # read notified events and enqeue them notifier.read_events() # you can do some tasks here ... except KeyboardInterrupt: # destroy the inotify's instance ont this interrup # stop monitoring notifier.stop() break