2

Per the EXAMPLES section in the lsof(8) man page on manpages.ubuntu.com, I should be able to run a command/take action if a process has a file open:

To take action only if a process has /u/abe/foo open, use:

lsof /u/abe/foo echo "still in use" 

When I try this syntax (in repeat mode), it doesn't work:

$ lsof -r /u/abe/foo tput bel lsof: status error on tput: No such file or directory lsof: status error on bel: No such file or directory ======= 

I reviewed the man page, but it's lengthy and I guess I've overlooked something.

What am I missing?

4
  • 1
    The TL;DR from Eduardo Trápani’s answer is that this is an error in that copy of the man page. I guess that it was meant to say lsof /u/abe/foo && echo "still in use", and a pre-processing step choked on the & characters.  Note that man page EXAMPLES are meant to be just that: examples /illustrations of what the main body of the page says. I see nothing in the page that talks about putting another command on the lsof command line. There are references to lsof’s exit status (I concede that they are obscure and cryptic). … (Cont’d) Commented Feb 13, 2024 at 22:44
  • (Cont’d) … BTW, I find that The Linux man-pages project at man7.org (© 2024 Michael Kerrisk) generally has the best man pages. Commented Feb 13, 2024 at 22:44
  • @G-ManSays'ReinstateMonica' I concur! Feel free to add your comment as an answer as your solution works as well! Or I can! Commented Feb 13, 2024 at 23:47
  • The only part of my comment that works as a solution is already present in Eduardo Trápani’s answer. Commented Feb 14, 2024 at 1:24

1 Answer 1

2

Even though the manpage lists it, that is not a valid lsof invocation. lsof will consider echo and still in use as names:

lsof /u/abe/foo echo "still in use" 

To have a result from lsof and act on it you could use the exit code. In bash:

lsof filename && echo "still in use" 

or:

lsof filename || echo "not in use" 

If you want this to happen repeatedly you can put the above code in a loop or put lsof in repeat mode and then process its standard output, for example in bash:

lsof -t -r2 filename | grep --line-buffered -v '=======' | while read pid do echo Process $pid is using the file done 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.