aspose file tools*
The moose likes Linux / UNIX and the fly likes check whether files are used by other process Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "check whether files are used by other process" Watch "check whether files are used by other process" New topic
Author

check whether files are used by other process

Gopu Akraju
Ranch Hand

Joined: Jan 13, 2008
Posts: 242
Hi,

I am trying to delete a folder which consists of 8 folders which in turn consists of few .tiff images. Before I delete these images recursively using "rm -rf" command, I would like to know whether these files are being used by some other processes or typically perl and python programs specifically.

I have a simple solution in my mind to check whether these files are used by other process. I will try to rename this folder. If it allows, then these files are used by other process and hence I can delete these folders recursively. If it doesn't allow me change the name, then the .tiff files are being used by some other process and hence I can exit without deleting the folders.

Please let me know whether this approach is correct or any other graceful way of checking whether the folders are being used by other process.

Thanks.
Satish Chilukuri
Ranch Hand

Joined: Jun 23, 2005
Posts: 266
Renaming will not work as Linux allows that even if the file is open. lsof may be what you are looking for.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16303
    
  21

Satish is correct. Linux is much less tenacious in file locking than Windows is.

However, Murphy's Law says that in the few microseconds between the time you do the check and the time you delete, someone will manage to come in on another process and grab the file, so a simple "lsof/rm" isn't reliable.

Depending on context, it may or may not be a problem if you remove a file from the filesystem while another app is using it, since the actual file contents aren't deleted, just the directory linkage - the contents don't formally disappear until the user has closed the file.

However, if that's a problem, you need to add some sort of monitoring construct that allows the files to be locked and unlocked and add it to both the apps that use it and to the app that deletes them. The monitor would maintain a semaphore to serialize access.

A variant approach in modern-day Linux might be to create a userspace filesystem that fronts the actual files in question and does all that work for you. That way you would require minimal-to-no coding changes to the apps.

Please don't ask me for examples. It's more work that I can afford to do for free - especially in today's economy. But it's doable with a moderate amount of work.


Customer surveys are for companies who didn't pay proper attention to begin with.
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
If you can afford to re-code the scripts slightly, a separate file lock might help you? In Bash you'd be looking at something like:A similar approach in the Perl (or whatever) scripts will tell this external script when the directory is in use via the LOCK_FILE and cause it not to remove files. It doesn't however provide for concurrent access from multiple simultaneously executing Perl scripts, nor provide any indication of how many scripts are currently using the directory. For that you'd need some sort of counter, or possibly a directory containing lock files with randomly generated names (one for each Perl script execution).

It will almost always work, except if the processor allows execution of the same lines in your foreign script between the if and touch lines, which would be most unfortunate (and highly improbably).

That just came from the top of my head---it might not be the best solution to your problem. Does it help at all?
[ September 29, 2008: Message edited by: Charles Lyons ]

Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
And after I pressed submit, I realised you can use Linux native file locks, which are atomic operations so will be immune to multi-threading. You'll need to use flock---e.g. see here for shell command and here for Perl. That should save you some work and in theory be better too...
Gopu Akraju
Ranch Hand

Joined: Jan 13, 2008
Posts: 242
Thanks everyone for your help and will start my home work. Thanks.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16303
    
  21

I was going to call Charles out on the locking, but he caught it.

The link for flock was for man(2) flock, which is an API function. The actual page for the shell-script equivalent is man(1) flock.
Masoud Kalali
Author
Ranch Hand

Joined: Jul 08, 2004
Posts: 531

Maybe you could use a simple linux command to see which files are in use by which process. for example if you put the parent folder name in the following command you will find which files inside it or its child are in use by some process.



Masoud Kalali
Software Engineer - My Weblog - GlassFish Security
nitin ginotra
Greenhorn

Joined: Aug 31, 2005
Posts: 1
I have a java program that looks for files in a folder on a linux machine. The files in the folder are being ftp’ed. I have a requirement of not processing these files till the time they are being written to that folder. I tried using FileChannel and locking but linux is much less tenacious as far as locks are concerned.

Is there a way of knowing that the file is still being written/copied to the location through Java on linux systems?
vandana awat
Greenhorn

Joined: Mar 15, 2009
Posts: 1
Hi nitin
did you find a solution to your problem. I'm facing a similar issue. Need to check if the ftp'ed file is complete before accessing it. Please let me know the solution for the same.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: check whether files are used by other process