LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-24-2011, 07:46 AM   #1
hk_centos
LQ Newbie
 
Registered: Jun 2011
Posts: 22

Rep: Reputation: Disabled
Need an lternative for lsof command


Hi,

In my bash script I need to move files in a folder if it is not in use.

Code:
for entry in `ls /root/shared_storage/input`; do

                echo $entry
		run=`lsof /root/shared_storage/input/$entry` 
		ru=${run:0:5}
		echo $entry

		if [ "$ru" == "" ]; then
                    ........
It worked fine sometimes but sometimes it just get stuck at lsof.
Is there any other way that I can use here to check if the $entry is using some other process?
Or am I using this wrong?

Appriciate your ideas
 
Old 06-24-2011, 10:16 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Not sure what you mean by 'it worked'. According to my logic, the variable '$ru' can only ever get the value 'COMMA'.

You don't need the initial 'ls' command:
Code:
for entry in /root/shared_storage/input/*; do
If the filename in '$entry' contains whitespace, it will cause lsof to fail, so enclose the filespec in quotes:
Code:
run=$(lsof "$entry")
Note also the use of the '$( .... )' notation which is preferred over `backticks`.

On any files/directories that I tried, lsof always reports with a heading line:
Code:
COMMAND     PID USER   FD   TYPE DEVICE SIZE/OFF     NODE NAME
, so the result of the bash substring extraction is always 'COMMA'.

What is this intended to accomplish? It isn't obvious to me from the given context.
An alternative algorithm might be to parse the output of lsof (no args).

--- rod.

Last edited by theNbomr; 06-24-2011 at 10:18 AM.
 
1 members found this post helpful.
Old 06-24-2011, 11:56 PM   #3
hk_centos
LQ Newbie
 
Registered: Jun 2011
Posts: 22

Original Poster
Rep: Reputation: Disabled
Hi,

I don't need the output of lsof, what I need to know is if the file $entry is using by another process.
If it is in use it lsof gives out something otherwise nothing. When I catch the output of lsof to $run & compare it to be empty in if statement it tells something string too long. That's the only purpose of $ru value.
If $entry not in use it should be moved to somewhere else.

Hope I explained clear now & you can help me..
Thanks!
 
1 members found this post helpful.
Old 06-25-2011, 12:18 AM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
So your problem is that sometime lsof won't return "at all"?
It just hangs, and your loop doesn't get executed?

Btw, you wouldn't really need to capture any output:
Code:
for entry in /root/shared_storage/input/*; 
do
	if lsof $entry >/dev/null 2>&1; then


Cheers,
Tink
 
1 members found this post helpful.
Old 06-25-2011, 12:51 AM   #5
hk_centos
LQ Newbie
 
Registered: Jun 2011
Posts: 22

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Tinkster View Post
So your problem is that sometime lsof won't return "at all"?
It just hangs, and your loop doesn't get executed?

Btw, you wouldn't really need to capture any output:
Code:
for entry in /root/shared_storage/input/*; 
do
	if lsof $entry >/dev/null 2>&1; then


Cheers,
Tink
Exactly!
It just hungs there at lsof (doesn't print the 2ns $entry in code).

Do you know why it happens?
Any solutions for that?
 
Old 06-25-2011, 01:09 AM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
I've seen lsof take its time, I never saw it fail ... on such an
occasion where it fails, can you check with strace what it's doing?

strace -vFf -o ~/lsof.out -p <lsof PID>


If you don't want to go down the route of finding out why lsof
is failing, you could use "fuser" instead.
Code:
for entry in /root/shared_storage/input/*; 
do
	if fuser $entry >/dev/null 2>&1; then


Cheers,
Tink
 
1 members found this post helpful.
Old 06-29-2011, 10:30 AM   #7
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
Originally Posted by hk_centos View Post
Hi,

I don't need the output of lsof, what I need to know is if the file $entry is using by another process.
If it is in use it lsof gives out something otherwise nothing. When I catch the output of lsof to $run & compare it to be empty in if statement it tells something string too long. That's the only purpose of $ru value.
If $entry not in use it should be moved to somewhere else.

Hope I explained clear now & you can help me..
Thanks!
So, why not just grep the output of lsof?
Code:
for entry in /root/shared_storage/input/*; do
    lsof | grep "$entry"
    if [ $? -eq 0 ]; then
        echo Found $entry
    fi
--- rod.
 
Old 06-29-2011, 01:41 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by theNbomr View Post
So, why not just grep the output of lsof?
Code:
for entry in /root/shared_storage/input/*; do
    lsof | grep "$entry"
    if [ $? -eq 0 ]; then
        echo Found $entry
    fi
--- rod.
That's even worse :}

His problem is that sometimes lsof doesn't return. That will be
the same (or worse) if he gets ALL open files, and greps for the
entry. Plus it would increase the runtime if it runs to completion.


Cheers,
Tink
 
Old 06-29-2011, 04:26 PM   #9
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
It seems probable to me that the argument being passed to lsof has something to do with why lsof is misbehaving. Running it without any argument gets around that. It could be run once only, the output saved to a file, and the file parsed iteratively for each of the files in the specified directory. It may require a bit more careful parsing that my simple example, but that is a minor change dictated by the purpose. I just don't buy that lsof is somehow broken. I'm not saying that using fuser isn't a reasonable approach to the problem; rather that the way in which lsof was used by the OP seems suboptimal.

--- rod.
 
Old 06-29-2011, 04:45 PM   #10
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
If lsof doesn't return when invoked w/ a file/directory path as
an argument it's broken as far as I'm concerned. The only valid
reason for it to fail would be that a mountpoint/fs has become
unavailable, e.g., an NFS share went down, an HDD died. In these
cases an invocation w/o parameters would fail in the same way: it
just won't return.



Cheers,
Tink
 
Old 07-03-2011, 02:57 AM   #11
hk_centos
LQ Newbie
 
Registered: Jun 2011
Posts: 22

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Tinkster View Post
If lsof doesn't return when invoked w/ a file/directory path as
an argument it's broken as far as I'm concerned. The only valid
reason for it to fail would be that a mountpoint/fs has become
unavailable, e.g., an NFS share went down, an HDD died. In these
cases an invocation w/o parameters would fail in the same way: it
just won't return.



Cheers,
Tink
Hey Tink you are great! the directory /root/shared_storage/ contains some directories mounted to NFS. And moving input directory to /root/ almost solved the problem.

Thanks a million to you all for making me like Linux!!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to read lsof output command many file open by root ??? b-RAM Linux - Newbie 2 10-05-2009 03:40 AM
using lsof command rheosiva Linux - Newbie 2 04-02-2009 02:36 AM
using lsof command rheosiva Linux - Security 1 04-01-2009 09:56 AM
lsof: command not found ionian2k Linux - Newbie 1 12-26-2002 09:59 PM


All times are GMT -5. The time now is 10:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration