0

I'm scripting a special program to my company.

By using Inotifywait from inotify-tools, I'm watching a specific folder for new items, and as soon a new file appears, it will be encrypted with gpg and moved to another folder for further treatment.

For a single file, it works fine, but I noticed a problem: When a new file enters while another one is being processed, he is ignored and intotifywait don't treat it, so he stays stucked in the folder. Is there any way to handle multiple files at the same time?

Here is the code I have so far:

origin=/BRIO/QPC/conclu01/Criptografar
output=/BRIO/QPC/conclu01/GPG
finished=/BRIO/QPC/conclu01/Concluido


while true; do
    inotifywait -e create -e moved_to -e close_write -e moved_from $origin --exclude ".*(\.filepart|gpg|sh)" | while read dir event file
        do
            echo $event
        
        if [ "$event" == 'CLOSE_WRITE,CLOSE' ] || [ "$event" == 'MOVED_TO' ] || [ "$event" == 'CREATE' ]
            then
                    echo "Found the file $origin/$file, starting GPG"
            sleep 5
            gpg --encrypt --recipient Lucas --output "$output/$file.gpg" "$origin/$file"
            echo "The file $file was succesfully encrypted to $output/$file.gpg"
            mv -f "$origin/$file" $finished
            echo "The file $origin/$file was moved"
        fi
        done
done

1 Answer 1

2

Don't run inotifywait repeatedly, run it once in the monitor mode and read from its output:

inotifywait -m ... |
    while read dir event file ; do
        ...
    done
2
  • It worked perfectly, thank you very much for your help! Nov 11, 2022 at 13:53
  • fatrace is a LOT better. Nov 11, 2022 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.