Eike
Eike

Reputation: 470

bash script inotifywait wait for file completely written before proceding

I am trying to setup a tandem of a watch script and a pdf merge script to check a folder where my scanner writes pdfs to. The idea of the first script is to watch the folder until two pdfs are present then invoke the second script. Now the problem is that inotifywait detects when the files are created and as expected and the second script is invoked but especially with larger pdf this creates a failure as a pdf might still be written to by the scanner. I added the close_write event to inotifywait but I assume this is ignored as the create event is already true. I added a sleep as workaround but this is not only ugly it will fail on even bigger files that exceed a fixed timeout. What would be the best way to check if both files are completely written. Thank you.

Foldercheck script:

#! /bin/bash
n=0
inotifywait -m -e create,close_write $1 | while read line
do
    n=$(find $1 -type f | wc -l);
    echo $n
    if [ "$n" -eq "2" ] ; then 
      echo "found $n files"
      sleep 5
      ./pdfMergeMove.sh $1 cleanup
      n=0
    fi;
done 

Upvotes: 5

Views: 5439

Answers (1)

Eike
Eike

Reputation: 470

As pointed out by pacholik close_write as event option alone is sufficient:

inotifywait -m -e close_write $1 | while read line

Upvotes: 6

Related Questions