Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I'm very new to bash scripting, so I might be barking up the wrong tree, but here is my current situation:

In a script, I start soffice; to convert an odt to a pdf

It seems that soffice detaches itself from the script's process, and wanders off to do its own thing.. Okay, that's fine; GUI apps tend to do that ...

However I need to wait until the new pdf has been fully written, as my next step involves processing that new pdf.

(As far as I know) I can't utilize soffice's process-ID, because it may have already been a running process before my script started: eg. soffice may be already open for normal GUI activity for another unrelated document/presentation/spreadsheet.

The crux of the matter for me is that before I can proceed, the writing of the new pdf must be finalized...

Is there some way to determine when a file is no longer open to another process in "write" mode? ...

share|improve this question
add comment

1 Answer 1

up vote 11 down vote accepted

You can use lsof | grep /absolute/path/to/file.txt to see if a file is open. If the file is open, this command will return status 0, otherwise it will return 256 (1).

Be aware that this command will take a second since there are, normally, a lot of files open at any time.

You can also use lsof -c gedit, for example, to see which file gedit has opened. Restricting the output to one process will reduce execution time to practically nought.

Here's a script to wait:

#!/bin/bash

while :
do
    if ! [[ `lsof -c python3.2 | grep test.py` ]]
    then
        break
    fi
    sleep 0.5
done
echo "done"

This runs while a process 'pyhton3.2' has got the file 'test.py' opened. As soon as the file is closed, it moves on to 'echo done' and exits.

I've put 'sleep 0.5' in there so that the loop doesn't hog the CPU all that badly. Otherwise, it will use 100% cpu.

Bonus There seems to be an easy way to convent odt to pdf:

Thanks to scls19fr on the OOo Forum for this lovely tip. You can convert OpenOffice Writer files to PDF from the command line using unoconv -f pdf input.odt To get unoconv, simply run sudo apt-get install unoconv at the terminal. (rhyshale of rhyshale.wordpress.com)

share|improve this answer
    
Wrap this in an until loop and you've got a deal. –  Nick Pascucci Nov 21 '10 at 5:36
    
Done that. :-) I hope it's not terrible style - haven't bothered to read up on best practices. It works well anyway. –  Stefano Palazzo Nov 21 '10 at 6:25
    
Thanks Stefano... yes, it does work... so it is a good and correct answer.. One a side note, it has highlighted that what is happening in soffice is not as I thought.. It seems that a temporary file is being created and the final file is (perhaps/probably) renamed upon completion... So I'm now looking at that... in which case, I may just need to wait for its *existance... ... and thanks for the link on unoconv .. I'll look into it.. (overall, I'm converting .pdf to multiple ..jpegs one per page) –  Peter.O Nov 21 '10 at 6:56
    
@fred.bear to wait until a file exists, replace the if-statement with this: if [ -f test.odf ]. (bash script can be so cryptic sometimes...) –  Stefano Palazzo Nov 21 '10 at 15:19
1  
try lsof -- /path/to/file instead to avoid the grep'ing –  RubyTuesdayDONO Dec 24 '13 at 4:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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