Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using watchdog of python to monitoring afp shared folder from linux

I want linux machine(Raspberry pi) to monitor a shared folder by AFP(Apple file protocol, macbook is host).

I can mount shared folder by mount_afp, and installed watchdog python library to monitor a shared folder. The problem is that watchdog can monitor only modifications from linux machine itself.

If a monitoring folder has been modified by the host machine(Apple macbook) or other pc, linux machine couldn't find out the change. No logs came out.

After I tested the same watchdog python file in host machine(Apple macbook), I can get every logs of modifications by other machine.

Host machine can get every modifications of file or folder. But other machine(guest machine) can not monitor modifications of file from host or others.

Is it normal status of watchdog? Or is there any problem in account and permission?

Here is watchdog sample.

  import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler


class Watcher:
    DIRECTORY_TO_WATCH = "/path/to/my/directory"

    def __init__(self):
        self.observer = Observer()

    def run(self):
        event_handler = Handler()
        self.observer.schedule(event_handler, self.DIRECTORY_TO_WATCH, recursive=True)
        self.observer.start()
        try:
            while True:
                time.sleep(5)
        except:
            self.observer.stop()
            print "Error"

        self.observer.join()


class Handler(FileSystemEventHandler):

    @staticmethod
    def on_any_event(event):
        if event.is_directory:
            return None

        elif event.event_type == 'created':
            # Take any action here when a file is first created.
            print "Received created event - %s." % event.src_path

        elif event.event_type == 'modified':
            # Taken any action here when a file is modified.
            print "Received modified event - %s." % event.src_path


if __name__ == '__main__':
    w = Watcher()
    w.run()
like image 396
Jade Lee Avatar asked Dec 04 '22 21:12

Jade Lee


1 Answers

For network mounts, the usual filesystem events don't always get emitted. In such cases, instead of using Observer, try using PollingObserver -- i.e., change from:

   self.observer = Observer()

to

   from watchdog.observers.polling import PollingObserver
   ...
   self.observer = PollingObserver()

There is also a PollingObserverVFS class you could experiment with.

Documentation: https://pythonhosted.org/watchdog/api.html#module-watchdog.observers.polling

like image 138
user2524973 Avatar answered Dec 11 '22 17:12

user2524973