Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Inotify for a list of folders

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?

like image 517
Sumesh Avatar asked Nov 08 '22 18:11

Sumesh


1 Answers

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?

like image 98
jmunsch Avatar answered Nov 15 '22 07:11

jmunsch