Skip to content

Instantly share code, notes, and snippets.

@ls0f
Last active November 23, 2015 09:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ls0f/f2f3c5b048c3da7d2620 to your computer and use it in GitHub Desktop.
Save ls0f/f2f3c5b048c3da7d2620 to your computer and use it in GitHub Desktop.
daemon notify
import functools
import sys
import pyinotify
import datetime
def restart():
pass
class MyEventHandler(pyinotify.ProcessEvent):
def my_init(self, file_object=sys.stdout):
"""
This is your constructor it is automatically called from
ProcessEvent.__init__(), And extra arguments passed to __init__() would
be delegated automatically to my_init().
"""
self._file_object = file_object
def process_IN_DELETE(self, event):
"""
This method processes a specific type of event: IN_DELETE. event
is an instance of Event.
"""
self._file_object.write('deleting: %s\n' % event.pathname)
self._file_object.flush()
def process_IN_MODIFY(self, event):
self._file_object.write('modifing: %s at %s\n' % (event.pathname, str(datetime.datetime.now())))
self._file_object.flush()
restart()
def process_IN_CLOSE(self, event):
"""
This method is called on these events: IN_CLOSE_WRITE and
IN_CLOSE_NOWRITE.
"""
self._file_object.write('closing: %s\n' % event.pathname)
self._file_object.flush()
def process_default(self, event):
"""
Eventually, this method is called for all others types of events.
This method can be useful when an action fits all events.
"""
self._file_object.write('default processing\n')
self._file_object.flush()
wm = pyinotify.WatchManager()
notifier = pyinotify.Notifier(wm, MyEventHandler())
wm.add_watch('/tmp/test.txt', pyinotify.ALL_EVENTS)
try:
notifier.loop(daemonize=True,pid_file='/tmp/pyinotify.pid', stdout='/tmp/pyinotify.log')
except pyinotify.NotifierError, err:
print >> sys.stderr, err
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment