[Gllug] Checking if a file has finished being written?

Tethys tet at accucard.com
Mon Jan 27 15:20:11 UTC 2003


Robert McKay writes:

>#!/bin/bash
>FILE=test
>RET=0
>while [ "$RET" != "1" ]; do
>echo "Waiting for file to stop being used.."
>/sbin/fuser -s $FILE
>RET=$?
>done
>echo "pounce!"

Yikes! Stick a sleep in that loop, or you'll be spinning round doing
nothing but eating CPU time. Also fuser is only guaranteed to return
non-zero, not specifically 1, so the check should be:

	while [ $RET -eq 0 ]

BTW, fuser will only tell you that a file is open. lsof will tell you
if it's opened for writing. The downside is that lsof consumes more
resources than fuser.

Also, note that fuser lies. For example:

	isengard:~# umount /mnt/win
	umount: /mnt/win: device is busy
	isengard:~# fuser -m /mnt/win
	/mnt/win:             9744c
	isengard:~# kill -9 9744 
	isengard:~# fuser -m /mnt/win
	isengard:~# umount /mnt/win
	umount: /mnt/win: device is busy

Alternatively, you could check the last modification time of the file.
If it hasn't changed in n minutes, then you deem the file to be complete.
Obviously, the value of n chosen is a tradeoff between accuracy and
responsiveness.

Tet

-- 
Gllug mailing list  -  Gllug at linux.co.uk
http://list.ftech.net/mailman/listinfo/gllug




More information about the GLLUG mailing list