Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to tell if file is in use?

Status
Not open for further replies.

BrianAtWork

Programmer
Apr 12, 2004
148
US
Hello All,

I have a korn shell script that I use on an AIX machine to notify me once a group of files have arrived on the machine.
The script will run and check for the files every 10 minutes - during the check to see if the files are present, there is also a check to see if the file is in use or not - in other words, I don't want to count the file as being "present" if the file is not completely there yet.

Right now, I use the "fuser" command to tell whether a file is in use, but it is pretty slow, and there are over 200 files that need checking.
I use it like this:
Code:
INUSE=$(fuser $File 2>/dev/null | cut -d: -f2)
and then check the $INUSE variable for that file. The $INUSE variable will contain any process ids that are using the file. If the $INUSE variable is empty then I know the file isn't being used (hopefully).

Does anyone have a better idea of how to make sure the file is not being written to? I could do an "ls -l" to get the filesize, then check it again a couple of seconds later to see if the size changed, but then I am putting loops inside of loops...

Any ideas would be appreciated!

Thanks in advance!

Brian
 
You may consider lsof

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Good point - I ran lsof, and it found 780 file that were open. There is a lot of use on this box, so there are many open processes and many open files as well (/dev/hdisk2, /dev/ttyp1, /dev/pts/247 count as open files as well). I definitely would not want to run lsof for each of the 200+ files I am waiting for. Maybe I could run it once each 10 minutes and grep through the list.

I'm not quite sure how lsof works, so I tried to give it a filename, and got this:
Code:
lsof /tmp/myfiletocheck
lsof: WARNING: compiled for AIX version 4.3.3.0; this is 5.2.0.0.
Segmentation fault(coredump)

Maybe it's not compiled right either.
 
And what about lsof -h ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Well, you'll need to get lsof for AIX52 first. That would probably get rid of the seg-fault. The warning you get is kind of obvious...


HTH,

p5wizard
 
I will have to email the system admin to see if the newer version of lsof can be installed. I ran lsof -h and found the following:
Code:
lsof +|-r [t] repeat every t seconds (15); + until no files, - forever
That looks like what I want to do as well. Unfortunately none of this is an option until lsof is upgraded to the newer version.
 
Have you tried using fuser just once rather than looping through the files, i.e. fuser * > /tmp/fuser.out and then using grep filename /tmp/fuser.out to check whether each individual file is in use?

Doing a grep through a small text file for each file would use far less system resources and run much more quickly.

If the list of filenames is very long you may need to use ls | xargs fuser > /tmp/fuser.out instead.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top