Operating System - HP-UX
1832907 Members
3909 Online
110048 Solutions
New Discussion

Identify files in use (lsof or another method)

 
MAD_2
Super Advisor

Identify files in use (lsof or another method)

I am trying to help tune a script written in shell (ksh) which is taking way to long because it runs a few times "lsof".

It's plain using the following:
/usr/local/bin/lsof

The files to be identified have the following criteria:
1) Each time the files in question are to be checked if they are being used at a particular time, they are all being checked in a particular location. Example:
Files to be checked if they are in use are located in /home/user1/ or /home/user2 or /home/user3 (the script is checking each location to do some archiving for files that meet certain criteria).

2) Currently, the lsof is being used which causes a very lenghty search, each time it runs it runs over 1 min. per iteration, causing huge delays and amounts of CPU usage... We are not interested in checking ports names, other users, etc... Just the files currently in use and owned by the particular user in question.

Andy ideas of any other method, lsof would be fine if it did not take such a long time 1-2 min.

MAD
Contrary to popular belief, Unix is user friendly. It's just very particular about who it makes friends with
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor

Re: Identify files in use (lsof or another method)

Hi:

In lieu of 'lsof' you can use 'fuser' to determine what (if any) processes are using a file and how:

http://www.docs.hp.com/en/B2355-60127/fuser.1M.html

Regards!

...JRF...
Sandman!
Honored Contributor

Re: Identify files in use (lsof or another method)

Hi,

Well then you can slim-down the lsof command by removing the unwanted stuff from it and focussing only on what is desired. This will certainly reduce the running time of lsof

cheers!
Antonio Cardoso_1
Trusted Contributor

Re: Identify files in use (lsof or another method)

Hi,

if the number of files in /home/userx is not too important, runnning a command such as

find /home/userx -exec fuser {} \;
or
lsof /home/userx -exec lsof {} \;

may reduce the time needed to check files.

What about using repeated mode of lsof (lsof -r ): does it bring some improvement in your situation ?
MAD_2
Super Advisor

Re: Identify files in use (lsof or another method)

Thanks everyone for your responses!

James, one of my colleagues had already mentioned the possible solution you mentioned, fuser... However, this would require extensive rewriting of the script, and I would rather find a way to keep usage of lsof if possible.

Recently I tried some tests with the '-u' option (lsof -u ), I think that may speed up the searching of used files, have to modify the script to test better.

Antonio, I have to look into your suggestion, I'll assign points later. And Sandman, any examples (I am only intersted in files being used and nothing else-- no ports, etc)?

Thanks again everyone, I hope to be able to close this thread as soon as I have a feasible solution.
Contrary to popular belief, Unix is user friendly. It's just very particular about who it makes friends with
A. Clay Stephenson
Acclaimed Contributor

Re: Identify files in use (lsof or another method)

A far wiser approach would be to do all of this yourself via a lock file. The idea is that rather than using lsof or fuser, you simply create a separate file that serves to indicate that all of these files are in use. Another technique is to create a lock directory and let each process create a lock file when started and remove a lock file when finished. Only if the directory is empty, are the files considered not in use. This is actually the standard UNIX idiom for flagging files in use.
If it ain't broke, I can fix that.