Re: [SLUG] bash challenge

2008-04-12 Thread david
On Fri, 2008-04-11 at 15:58 +1000, Peter Hardy 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 21 ; do
   sleep 1
   done
 done
 

Thanks everyone.
For the archive, this is where I finished up - seems to work so far:

while inotifywait -e close somefile; do
some stuff, including a short sleep [1] 
while lsof foo/*.tif  /dev/null ; do
sleep 10# because they are big files
done
rm foo/*.tif
done

[1]
I wonder about timing issues. somefile is a 3 byte counter which is
the first thing updated by another program, which then creates the tif
files. I want my script to do it's thing after all that.

What happens if the lsof test happens before the tif files are first
opened for writing? Am I worrying too much?

I've put a sleep into some stuff in case that's a problem. For my job
it doesn't matter if there is a sleep in there.

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


Re: [SLUG] bash challenge

2008-04-12 Thread Amos Shapira
On Sat, Apr 12, 2008 at 6:06 PM, david [EMAIL PROTECTED] wrote:
  Thanks everyone.
  For the archive, this is where I finished up - seems to work so far:

  while inotifywait -e close somefile; do
 some stuff, including a short sleep [1] 
 while lsof foo/*.tif  /dev/null ; do
 sleep 10# because they are big files
 done
 rm foo/*.tif
  done

  [1]
  I wonder about timing issues. somefile is a 3 byte counter which is
  the first thing updated by another program, which then creates the tif
  files. I want my script to do it's thing after all that.

  What happens if the lsof test happens before the tif files are first
  opened for writing? Am I worrying too much?

  I've put a sleep into some stuff in case that's a problem. For my job
  it doesn't matter if there is a sleep in there.

Have you looked into tracking the processes instead of the files? Can
you identify a single process who's life-span matches the time that
the files are being created?

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


Re: [SLUG] bash challenge

2008-04-11 Thread Peter Hardy
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 21 ; do
sleep 1
done
done

(usual disclaimer about actually testing this first applies)

-- 
Pete

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


Re: [SLUG] bash challenge

2008-04-11 Thread Amos Shapira
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 21 ; 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


[SLUG] bash challenge

2008-04-10 Thread david
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?

tia

David

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