How, in a script, can I determine if a file is currently being written to by another process?
Solution 1:
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 runsudo apt-get install unoconv
at the terminal. (rhyshale of rhyshale.wordpress.com)