ProfNimrod
ProfNimrod

Reputation: 4310

Ubuntu iNotify multiple folder

I have written a short bash script that sets-up iNotify for several folders, and then calls a Python script to upload the text from the created files to a webservice. The process works fine for the first folder in the list, but the create event does not seem to be firing for the others. I'm assuming I've structured my script in correctly. Any ideas? The code is basically:

#!/bin/bash
inotifywait -m --format '%w%f' -e create /Folder1 | while read FILE
do
    echo $FILE
    # upload file
    python /Scripts/UploadFileContents.py 'PAUR' $FILE
done
inotifywait -m --format '%w%f' -e create /Folder2 | while read FILE
do
    echo $FILE
    # upload file
    python /Scripts/UploadFileContents.py 'RACH' $FILE
done
inotifywait -m --format '%w%f' -e create /Folder3 | while read FILE
do
    echo $FILE
    # upload file
    python /Scripts/UploadFileContents.py 'CDR' $FILE
done
inotifywait -m --format '%w%f' -e create /Folder4 | while read FILE
do
    echo $FILE
    # upload file
    python /Scripts/UploadFileContents.py 'CHR' $FILE
done
inotifywait -m --format '%w%f' -e create /Folder5 | while read FILE
do
    echo $FILE
    # upload file
    python /Scripts/UploadFileContents.py 'PRMS' $FILE
done

Thanks in advance for any assistance.

Upvotes: 0

Views: 1305

Answers (2)

Impossibear
Impossibear

Reputation: 195

Your first while loop never ends. inotifywait -m is a command that runs forever.

On top of that inotifywait itself is blocking, so breaking all them out to one loop and removing the m flag will not help you either.

The simplest solution for you would be to break them out to multiple scripts.

Or more ideal would be to setup a single script that accepts a few parameters since most of the logic is repeated.

You could replace /Folder1 with $1 and your second variable with $2 Then call your script passing the folder you want to watch.

./watch.sh /Folder1 PAUR &
./watch.sh /Folder2 RACH &

You could make a second script that calls the first script with all your folders you want to watch, to run one script per folder.

Upvotes: 1

hek2mgl
hek2mgl

Reputation: 158250

You'll have to start the inotifywait scripts in backround as they will block until changes appear

Upvotes: 1

Related Questions