Operating System - HP-UX
1832362 Members
3043 Online
110041 Solutions
New Discussion

Is the file being accessed

 
Dermot Beirne
Frequent Advisor

Is the file being accessed

Hi,
Is there a 100% reliable method to checking if a file is being accessed in any way before moving or copying it, to ensure that you are moving or copying a file that is not still growing or changing.
Thanks,
Dermot
Happy is harder than money. Anyone who thinks money will make them happy, doesn't have money.
6 REPLIES 6
Robin Wakefield
Honored Contributor

Re: Is the file being accessed

Hi Dermot,

Try "lsof filename". This can be downloaded from:

http://hpux.connect.org.uk/hppd/hpux/Sysadmin/lsof-4.61/

or other porting sites.

Rgds, Robin.
Darrell Allen
Honored Contributor

Re: Is the file being accessed

Hi Dermot,

The standard UNIX command is fuser:

fuser /filename

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
harry d brown jr
Honored Contributor

Re: Is the file being accessed

Dermot,

As Robin has noted, lsof is the best tool to use not only for who has what file open, put it can be used to view what sockets users have open also.

live free or die
harry
Live Free or Die
Dermot Beirne
Frequent Advisor

Re: Is the file being accessed

I know about the lsof command, but I'd like to make the script totally portable. Also, I know about the fuser command, but I have heard that if, for example, a file is being appended to via an ftp session, and the session locks or pauses for 10 seconds and then resumes, that the process attached to the file is dropped and it appears that the file isn't being accessed, which, it isn't! I've also heard other stories regarding fuser which would bring it's reliability for this purpose into question. If it start a process to access a file, and then freeze it, is the process still attached to the file? Does fuser still see it. Are you confident that fuser is in fact very reliable. I need to be certain for the process in which it will be used.
Thanks again,
Dermot.
Happy is harder than money. Anyone who thinks money will make them happy, doesn't have money.
Patrick Wallek
Honored Contributor

Re: Is the file being accessed

How "totally portable" do you want this script to be. Do you mean "totally portable" between different HP-UX boxes? Or "totally portable" between different flavors of Unix?

If you are talking just HP-UX then I would seriously look at lsof. It is FREE and there are versions available for all current versions of HP-UX. Yes, you do have to install it on each machine, but it is a tool that no one should be without anyway.
Darrell Allen
Honored Contributor

Re: Is the file being accessed

Hi again,

As said, lsof is a better tool than fuser however fuser should tell you if the file is being accessed. I don't know why it would not be reliable.

You'll have to take into account how the file is being written. Nothing is fool-proof if the file is opened, written to, closed, then re-opened in append mode. Does ftp do that? I don't know. Can I append to a file after it has been written then closed? Absolutely.

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)