Operating System - HP-UX
1832910 Members
2746 Online
110048 Solutions
New Discussion

Re: Is the file already open by another process ?

 
SOLVED
Go to solution
Christophe MAILHE
Frequent Advisor

Is the file already open by another process ?

Hello,

Can someone tell me how I can check if a file is not already open by another process ?

I am using a pipe to communicate between 2 different script. 1 of the script is started from the inittab (user root) and read from the pipe, the other one start from the cron each 15 minutes (user oracle) and is writing to the pipe.

I need to make sure another process is not writing to the pipe at the begining of the script (to avoid 2 simultaneous run of the script for example).

Any idea ?

Many thanks,

Chris.
11 REPLIES 11
harry d brown jr
Honored Contributor

Re: Is the file already open by another process ?


From a script, which I assume isn't perl? then I'd use "lsof"

http://hpux.cs.utah.edu/hppd/hpux/Sysadmin/lsof-4.61/

w/perl you can check the status of a file to see if it is open already.

live free or die
harry
Live Free or Die
Steven Sim Kok Leong
Honored Contributor

Re: Is the file already open by another process ?

Hi,

# lsof | grep $FILENAME

Hope this helps. Regards.

Steven Sim Kok Leong
A. Clay Stephenson
Acclaimed Contributor

Re: Is the file already open by another process ?

Probably the most straighforward method is to use of lockfile (or a lock directory). As soon as a process starts using the pipe, create a directory, when finished remove it. If the directory already exists, exit immeditately because the other process is using it.
If it ain't broke, I can fix that.
MANOJ SRIVASTAVA
Honored Contributor

Re: Is the file already open by another process ?

Hi Christoph


LSOF is the utility which can help you .

ftp://vic.cc.purdue.edu/pub/tools/unix/lsof/

is the place to get it from , they ahve a binary version too , incase you cvan get it please do let me know and I will email it to you .


Manoj Srivastava
PIYUSH D. PATEL
Honored Contributor

Re: Is the file already open by another process ?

Hi,

You can download the lsof utility from :

ftp://vic.cc.purdue.edu/pub/tools/unix/lsof

then do a lsof | grep filename

Piyush
S.K. Chan
Honored Contributor

Re: Is the file already open by another process ?

Try "fuser" too though "lsof" is highly recommended.
# fuser -u

Man "fuser" for details.
Christophe MAILHE
Frequent Advisor

Re: Is the file already open by another process ?

Hi everybody,

Thanks for your answers.

I have used lock directories in the past, but the issue with that being : If a previous run did not terminate properly and the trap did not remove the directory (yes, that's happend to me) the next run never start !
So, I have tried another idea with a open file: If the file is still open, return, else open the file.

Impossible to use fuser as one of the user is not root.

I have already tried lsof, but look like taking a lot of ressources for a simple operation...

Any other suggestion
Tharangini Karthikeyan
Occasional Contributor

Re: Is the file already open by another process ?

Hi
Even I am facing the same problem.Now I have an idea.
Store the inode number of all the files whichever u open in list.
Doing a stat on the filename we can get the inode number.
Whenever you open a file check if the inode number is in the list.If it is there then it states that the file is open.
Note: Whenever the file is closed the corresponding inode number should be removed from the list.

I think this might solve the problem.
You might get a better solution.If so then do reply for my query.Subject of my query is
"How can I find the open status of the file?"

Regards
Tharangini


Klaus Crusius
Trusted Contributor
Solution

Re: Is the file already open by another process ?

Hi,

to solve the script co-ordination problem as well as the exclusive access problem, I made a solution using file locks.
A C-program takes a file lock (see man fcntl) on a dedicated regular file per resource, then execs the script (the name and arguments of which are arguments to the wrapper program). The file lock is relyably released iff the process exits.

Regards, Klaus
There is a live before death!
Sanjay_6
Honored Contributor

Re: Is the file already open by another process ?

Hi,

With lsof, just try,

lsof /path_to_file/file_name

Hope this helps.

Regds
Leon Allen
Regular Advisor

Re: Is the file already open by another process ?

I have found lsof impossible to install on our 64 bit hpux 11.0 machine.

I resolved my file-open issues with fuser (fuser -cu /u06)

Cheers!

Leon
Time's fun when your having flys (ancient frog saying)