Вопрос

I have a problem with pyinotify: the methods process_*() of the ProcessEvent are not called

The code

import sys, time, syslog
from pyinotify import WatchManager, Notifier, ThreadedNotifier, ProcessEvent, EventsCodes
from daemon import Daemon


class PTmp(ProcessEvent):
    def process_IN_CREATE(self, event):
        syslog.syslog("creating: " + event.pathname)
    def process_IN_DELETE(self, event):
        syslog.syslog("creating: " + event.pathname)
    def process_default(self, event):
        syslog.syslog("default: " + event.pathname)

class MyDaemon(Daemon):
    def run(self):
        syslog.openlog('archmind',syslog.LOG_PID,syslog.LOG_DAEMON)
        syslog.syslog('daemon started, entering loop')
        wm = WatchManager()
        mask = IN_DELETE | IN_CREATE
        notifier = ThreadedNotifier(wm, PTmp())
        notifier.start()
        wdd = wm.add_watch('/tmp', mask, rec=True)
        while True:
            time.sleep(1)
        wm.rm_watch(wdd.values())
        notifier.stop()
        syslog.syslog('exiting loop')
        syslog.closelog()

if __name__ == "__main__":
    daemon = MyDaemon('/tmp/archmind.pid')
    if len(sys.argv) == 2:
        if 'start' == sys.argv[1]:
            daemon.start()
        elif 'stop' == sys.argv[1]:
            daemon.stop()
        elif 'restart' == sys.argv[1]:
            daemon.restart()
        else:
            print "Unknown command"
            sys.exit(2)
        sys.exit(0)
    else:
        print "usage: %s start|stop|restart" % sys.argv[0]
        sys.exit(2)

Everything else is logged. inofity works correctly, I have tested it with inotifywait. What may I be missing?

Это было полезно?

Решение

The problem was missing the import of IN_*, which has to be done manually. I've found that out after calling

daemon.run()

instead of

daemon.start()

thus making the script run in foreground.

Другие советы

masks are defined in EventCodes.ALL_FLAGS

 mask = EventsCodes.ALL_FLAGS['IN_DELETE']
 mask = EventsCodes.ALL_FLAGS['ALL_EVENTS']
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top