Before I delete any file in Unix, How can I check no open file handle is pointing to that file? | Unix Linux Forums | Solaris

  Go Back    


Solaris The Solaris Operating System, usually known simply as Solaris, is a Unix-based operating system introduced by Sun Microsystems. The Solaris OS is now owned by Oracle.

Before I delete any file in Unix, How can I check no open file handle is pointing to that file?

Solaris


Tags
file handle

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-05-2010
kchinnam kchinnam is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 29 April 2014, 10:34 PM EDT
Location: US
Posts: 142
Thanks: 15
Thanked 2 Times in 2 Posts
Before I delete any file in Unix, How can I check no open file handle is pointing to that file?

I know how to check if any file has a unix process using a file by looking at 'lsof <fullpath/filename>' command.

I think using lsof is very expensive. Also to make it accurate we need to inlcude fullpath of the file.

Is there another command that can tell if a file has a truely active open file handle pointing to it ? or if a file has a runtime PID associated with it?
Sponsored Links
    #2  
Old 10-05-2010
DGPickett DGPickett is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 31 July 2014, 4:55 PM EDT
Location: Southern NJ, USA (Nord)
Posts: 4,390
Thanks: 8
Thanked 535 Times in 514 Posts
fuser, which is more commonly available than lsof, does this, putting the pids on stdout and the file names and flags on stderr, so you can get the pid list easily. For example:


Code:
$ for p in $( fuser . 2>/dev/null )
do
 echo
 ps -fp $p
 echo
 ptree $p
done
$


Last edited by DGPickett; 10-05-2010 at 12:12 PM..
Sponsored Links
    #3  
Old 10-05-2010
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 1 August 2014, 6:49 PM EDT
Location: Saskatchewan
Posts: 19,068
Thanks: 759
Thanked 3,173 Times in 2,976 Posts
Nothing bad happens if you delete a file in use. It just persists on disk until the last thing using it quits, then is deleted for good.

There's no faster way to query the kernel about open files, than querying the kernel about open files. fuser works roughly the same way as lsof, so isn't really a slimmer/better workaround.
    #4  
Old 10-05-2010
DGPickett DGPickett is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 31 July 2014, 4:55 PM EDT
Location: Southern NJ, USA (Nord)
Posts: 4,390
Thanks: 8
Thanked 535 Times in 514 Posts
If you want to delete a file and not have lost space if some program holds it forever, truncate it first. However, if the program appends with a high file position, all the space is reallocated and written to zeros, so there is some virtue in ensuring nobody has it open to write before you delete it.
Sponsored Links
    #5  
Old 10-05-2010
kchinnam kchinnam is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 29 April 2014, 10:34 PM EDT
Location: US
Posts: 142
Thanks: 15
Thanked 2 Times in 2 Posts
Thanks for pointing me to "fuser" command.

Does someone have a solaris equivalent of this script?
Sponsored Links
    #6  
Old 10-05-2010
achenle achenle is offline
Registered User
 
Join Date: Jun 2009
Last Activity: 1 August 2014, 9:14 AM EDT
Posts: 614
Thanks: 1
Thanked 79 Times in 75 Posts
Yes - it's "fuser".

And FWIW, assuming you're running on Solaris (as this is posted under the Solaris topic...), fuser on Solaris is a lot faster than lsof on Linux. The last time I looked at the lsof source code it was doing nothing more than searching through the entire /proc file system looking for a match to the file(s) it was given. The Solaris implementation of fuser operates fully inside the kernel (see /usr/include/sys/utssys.h on a Solaris box for the actual system call that does the work) and it seems to me that the Solaris kernel pretty much has to have a direct way of finding the processes that have that file open since it returns so fast - it seems way too fast to be searching unless what it's searching is really small.
Sponsored Links
    #7  
Old 10-05-2010
kchinnam kchinnam is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 29 April 2014, 10:34 PM EDT
Location: US
Posts: 142
Thanks: 15
Thanked 2 Times in 2 Posts
Quote:
Nothing bad happens if you delete a file in use.
My observation is different. If we delete "catalina.out" from a tomcat web server logs folder. Tomcat would not create another log,, until we restart tomcat. Where as other applications do create a new log file.

I am trying to find a safe way to do a generic log file rotation including logs like "catalina.out". Also try to know if a file is being actively used or not to make a better decision before log/file rotation.

When I query using lsof I get this -->
Quote:
> lsof ../jakarta/logs/catalina.out
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 1162 <UID> 1w VREG 314,7 1284251 19505649 ../jakarta/logs/catalina.out
java 1162 <UID> 2w VREG 314,7 1284251 19505649 ../jakarta/logs/catalina.out
What is the meaning of FD --> 1w 2w ?


2. What is the difference between 1w, 2w(in lsof output) Vs <PID>o (o in fuser output) ?

Quote:
> fuser -fu ../jakarta/logs/catalina.out
../jakarta/logs/catalina.out: 13601o(<userid>)

Last edited by kchinnam; 10-05-2010 at 01:32 PM.. Reason: masking secure info
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to create a user interface pointing to a file? laknar Shell Programming and Scripting 3 02-25-2010 05:02 AM
avoid open file to check field. jimmy_y Shell Programming and Scripting 4 10-30-2009 05:58 AM
Unable to delete an open file vikasrout UNIX for Advanced & Expert Users 11 03-25-2009 03:26 AM
delete open file delonism UNIX for Dummies Questions & Answers 2 01-14-2009 06:48 AM
Error Message: find: cannot open /: Stale NFS file handle ezsurf Solaris 1 10-11-2007 08:04 PM



All times are GMT -4. The time now is 08:22 PM.