5

I am looking for a way to check a list of document roots of 5 domains to inotify directory watch.

For a single folder it is working like this

DIRECTORY_TO_WATCH = "/home/serveradmin/"

I have a list of folders which needs to be checked recursively for my server.

I just started learning python and have some hands on experience in C language. Just starting learning developments.

Is there anyone who can help me on this? I need to have a recursive inotify watch on 5 folders mentioned in a file named /tmp/folderlist.txt

IS there any similar code available anywhere I can refer?

A Magoon
  • 1,180
  • 2
  • 13
  • 21
Sumesh
  • 51
  • 4

3 Answers3

1

install inotify-tools:

sudo apt-get install inotify-tools

and try something like:

inotifywait = ['inotifywait',
               '--recursive',
               '--quiet',
               '--monitor', ## '--timeout', '1',
               '--event',
               'CREATE',
               '--format', '%f']

from subprocess import PIPE, Popen
p = Popen(inotifywait + paths, stdout=PIPE)
for line in iter(p.stdout.readline, ''):
    print(line)

For macosx you can get similar results using fswatch:

if sys.platform == 'darwin':
    inotifywait = ['fswatch', '--event', 'Created']

For windows see : Is there anything like inotify on Windows?

jmunsch
  • 22,771
  • 11
  • 93
  • 114
0

So you want to create a list of the folders then do a for a statement to check if they exist.

So do

import os
list = ['folder1', 'folder2', 'folder3', 'folder4', 'folder5']
for i in list:
    print("\nDoes", list[i], "exsist?", os.path.exists("/home/serveradmin/" + list[i]))

Replace the items in list with the correct folder names!

Good luck!

James
  • 1,928
  • 3
  • 13
  • 30
  • 1
    The OP wants to use inotify so that they can be notified when a directory is changed. They do not want to check if a directory exists – Andrea Corbellini Aug 02 '17 at 04:45
  • Yes he is correct. This needs to be passed to inotify to keep an eye on file changes recursively – Sumesh Aug 02 '17 at 05:39
  • Any other options available for this ? also do any one know which is the most valid / useful c programming to python convertors ? Is there any thing like that – Sumesh Aug 05 '17 at 13:52
  • @Sumesh did you get it working? If so please mark my answer as correct so people in the future can fix this issue! – James Sep 16 '17 at 19:25
0

This is the sample code I have started working. I need to replace "DIRECTORY_TO_WATCH" to a list of folders which I will add in a file. So basically it will be a list of folders specified in a txt file so that I can just keep adding future folders in that file and no need to add the core code every time.

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"
Andrea Corbellini
  • 17,339
  • 3
  • 53
  • 69
Sumesh
  • 51
  • 4