6

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()
Jade Lee
  • 69
  • 2
  • 8
  • The watchdog can only monitor changes to the local file system. It is not notified by the OS and remote file system about changes. – Klaus D. Aug 01 '17 at 15:21
  • @Klaus D Hi, is there any python library to monitor AFP shared folder? – Jade Lee Aug 01 '17 at 16:29
  • The first question would be if the client is notified by the host. If not there won't be any library. – Klaus D. Aug 01 '17 at 16:30

1 Answers1

19

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

Abhimanyu
  • 725
  • 1
  • 6
  • 20
user2524973
  • 1,087
  • 6
  • 14
  • 5
    i tried from watchdog.observers import PollingObserver but import error. i check the doc, and change to from watchdog.observers.polling import PollingObserver and basically, its worked!!! – pinky Jun 02 '18 at 06:17
  • Code example has been updated to import from the correct module. Thanks everyone. – user2524973 Jun 06 '19 at 16:02