Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
python inotify daemon
import multiprocessing
import os
import time
from functools import partial
import pyinotify
def is_alive(pid):
try:
os.kill(pid, 0)
except OSError:
return False
else:
return True
class Handler(pyinotify.ProcessEvent):
def process_IN_CREATE(self, event):
print event.pathname
def worker(directory, parent_pid):
wm = pyinotify.WatchManager()
notifier = pyinotify.ThreadedNotifier(wm, Handler())
notifier.start()
wdd = wm.add_watch(directory, pyinotify.IN_CREATE)
waiting = True
while waiting:
if is_alive(parent_pid):
pass
else:
waiting = False
notifier.stop()
def main()
directory = '/tmp'
f = partial(worker, directory, os.getpid())
p = multiprocessing.Process(target=f)
p.daemon = True
p.start()
time.sleep(10)
exit()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment