• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

check whether files are used by other process

 
Gopu Akraju
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 17989
47
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone for your help and will start my home work. Thanks.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 17989
47
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 531
Java Mac OS X Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
nitin ginotra
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic