By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,279 Members | 1,673 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,279 IT Pros & Developers. It's quick & easy.

File is getting Locked. Pls help

P: 6
Hi
This is my first post to this forum.

I am struck up with a problem.

We have a perl script which deals with creation/deletion of files. This perl script is trigerred through a java webservice. But at times the files are getting locked by a process. We are not able to delete the files. the error message is " Cannot accoss the file <file name> because it is used by another process"

So pls help to resolve.

Note: When the restart the tomcat server which hosts the webservice, it is deleting the file. In my code I am closing the file handles correctly.
Jan 17 '08 #1
Share this Question
Share on Google+
8 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
Hi
This is my first post to this forum.

I am struck up with a problem.

We have a perl script which deals with creation/deletion of files. This perl script is trigerred through a java webservice. But at times the files are getting locked by a process. We are not able to delete the files. the error message is " Cannot accoss the file <file name> because it is used by another process"

So pls help to resolve.

Note: When the restart the tomcat server which hosts the webservice, it is deleting the file. In my code I am closing the file handles correctly.
Are you able to determine what is using the files you are trying to delete? Stopping the access to the files will help.

Regards,

Jeff
Jan 17 '08 #2

prn
Expert 100+
P: 254
prn
Are you able to determine what is using the files you are trying to delete? Stopping the access to the files will help.
On a *ix system, at least, you can probably determine what is locking the file with lsof. I'd suggest logging that information before you attempt to delete the file. Something like:
Expand|Select|Wrap|Line Numbers
  1. system ("lsof $filename >>$log");
ought to work for you.

If your system does not have lsof on it (most systems seem not to have it in the default installation) you may need to install it. If you are not the sysadmin, you'll need to get the sysadmin to do it as it requires privileges. However, lsof is something that every sysadmin ought to have in his/her bag of tricks. It's great, it's portable, and it is indispensible for situations like this. I've used it on Solaris and Linux, it is also available for just about any other flavor of unix or unix-like system.

Best Regards,
Paul
Jan 17 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
Maybe make deleting the file a recursive function and give it so many attempts before finally giving up.

Expand|Select|Wrap|Line Numbers
  1. sub delete {
  2.    my $file = shift;
  3.    my $n = shift || 1;
  4.    my $error = shift  || 'unknown error'; 
  5.    do_some_error("Can't delete $file: $error") if $n > 5;
  6.    unless (unlink ($file)) {
  7.       sleep (1); # sleeps for one second before trying again;
  8.       delete($file,++$n,$!);
  9.    }
  10. }

Of course this is a work-around and may not work at all.
Jan 17 '08 #4

P: 6
Hi
Thanks for the reply. I will try it out and get back to you.

On a *ix system, at least, you can probably determine what is locking the file with lsof. I'd suggest logging that information before you attempt to delete the file. Something like:
Expand|Select|Wrap|Line Numbers
  1. system ("lsof $filename >>$log");
ought to work for you.

If your system does not have lsof on it (most systems seem not to have it in the default installation) you may need to install it. If you are not the sysadmin, you'll need to get the sysadmin to do it as it requires privileges. However, lsof is something that every sysadmin ought to have in his/her bag of tricks. It's great, it's portable, and it is indispensible for situations like this. I've used it on Solaris and Linux, it is also available for just about any other flavor of unix or unix-like system.

Best Regards,
Paul
Jan 18 '08 #5

P: 6
Hi Paul

I am using windows 2000. Do you suggest me any utility in Windows to find out which process has locked my file. I am facing the File locking problem in a production server. So it will be better if there are things already available in the OS (Not need of downloading any extra thing)

Thanks for your help.

Regards
Finny

On a *ix system, at least, you can probably determine what is locking the file with lsof. I'd suggest logging that information before you attempt to delete the file. Something like:
Expand|Select|Wrap|Line Numbers
  1. system ("lsof $filename >>$log");
ought to work for you.

If your system does not have lsof on it (most systems seem not to have it in the default installation) you may need to install it. If you are not the sysadmin, you'll need to get the sysadmin to do it as it requires privileges. However, lsof is something that every sysadmin ought to have in his/her bag of tricks. It's great, it's portable, and it is indispensible for situations like this. I've used it on Solaris and Linux, it is also available for just about any other flavor of unix or unix-like system.

Best Regards,
Paul
Jan 28 '08 #6

KevinADC
Expert 2.5K+
P: 4,059
The file may just be open is the reason you can't delete it. Can we see the perl code?
Jan 28 '08 #7

P: 6
Hi
Thanks for the reply.

I am very sure that the file handlers are closed. I am closing the handlers at all cases. I have checked that multiple times.
(e.g)
open(STATUSLOG,">>"$StatusFile);
print(STATUSLOG "COMPLETED");
close(STATUSLOG);
Java webservice is invoking the perl scripts. It also does some operations with that file. I doubt whether java webservice is locking the file.

Inorder to confirm that, i need to find which is locking the file. That java code is not my code. Before pointing out my hand to others code, i am trying to have some valid point for me.

Thats why i have to find the process which has locked the file.

Regards
Finny
The file may just be open is the reason you can't delete it. Can we see the perl code?
Jan 28 '08 #8

KevinADC
Expert 2.5K+
P: 4,059
You have not checked anything really, you assume the open/close functions are successful, try this and see if you get any error messages:

Expand|Select|Wrap|Line Numbers
  1. open(STATUSLOG,">>"$StatusFile) or die "Can't open $StatusFile: $!";
  2. print STATUSLOG "COMPLETED";
  3. close(STATUSLOG) or die "Can't close $StatusFile: $!";
Jan 28 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.