Search code examples
shellinotifyinotifywait

inotifywait triggering event twice while converting docx to PDF


I have shell script with inotifwait set up as under:

inotifywait -r  -e close_write,moved_to  -m "<path>/upload" --format '%f##@@##%e##@@##%w'

There are some docx files residing in watched directory and some script converts docx to PDF via below command:

soffice --headless --convert-to pdf:writer_pdf_Export <path>/upload/somedoc.docx --outdir  <path>/upload/

Somehow event is triggered twice as soon as PDF is generated. Entries are as under:

somedoc.pdf##@@##CLOSE_WRITE,CLOSE##@@##<path>/upload/
somedoc.pdf##@@##CLOSE_WRITE,CLOSE##@@##<path>/upload/

What else is wrong here?

Regards


Solution

  • It's triggered twice because this is how soffice appears to behave internally. One day it may start writing it 10 times and doing sleep 2 between such writes during a single run, our program can't and I believe shouldn't anticipate it and depend on it.

    So I'd try solving the problem from a different angle - lets just put the converted file into a temporary directory and then move it to the target dir, like this:

    soffice --headless --convert-to pdf:writer_pdf_Export <path>/upload/somedoc.docx --outdir <path>/tempdir/ && mv <path>/tempdir/somedoc.pdf <path>/upload/

    and use inotifywait in the following way:

    inotifywait -r -e moved_to  -m "<path>/upload" --format '%f##@@##%e##@@##%w'
    

    The advantage is that you no longer depend on soffice's internal logic. If you can't adjust behavior of the script producing the pdf files then indeed you'll need to resort to a workaround like @Tarun suggested.