On Fri, Apr 11, 2008 at 3:58 PM, Peter Hardy <[EMAIL PROTECTED]> wrote:
> On Fri, 2008-04-11 at 15:06 +1000, david wrote:
>  > while inotifywait -e close somefile ; do
>  >       <lots of stuff>
>  >       rm foo/*.tif
>  > done
>  >
>  >
>  > The problem is that i need to make sure that foo/*.tif have closed
>  > before I remove them, or strange things happen.
>  >
>  > I don't know what the names of the tif files are going to be, although I
>  > know they will in directory foo/
>  >
>  > Is there a way of waiting until all files in foo/ are closed?
>
>  lsof will work as long as foo/*.tif doesn't overflow your command line
>  length.
>
>
>  while inotifywait -e close somefile ; do
>         <stuff>
>         while lsof foo/*.tif > /dev/null 2>&1 ; do
>                 sleep 1
>         done
>  done
>
>  (usual disclaimer about actually testing this first applies)

Haven't tested this but maybe you can make the inner loop even smarter
with the same tool you use in the outer loop - inotifywait can tell
you when a watched file is closed, so maybe something like:

while inotifywait -e close somefile ; do
       <stuff>
       while fuser -s foo/*.tif; do
           inotifywait -t 10 -e close foo/*.tif
       done
done

"fuser" allows for silent operation which is more appropriate here,
and it's my personal favourite.
The "-t 10" argument will tell inotifywait to exit after 10 seconds
whatever happens, increase this to the time range you expect the
programs to finish within. It's there to avoid a race condition where
the last file was closed between the "fuser" call and the inner
"inotifywait" call.

#include <std.disclaimer.not.tested>

Cheers,

--Amos
-- 
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html

Reply via email to