0

I found this command line is not working

ssh i01n10 "/usr/sbin/lsof -p $(pgrep -nf a.out)"

it shows the error

lsof: no process ID specified

However

ssh i01n10 "$(pgrep -nf a.out)"

correctly gives the PID

Why lsof is not seeing the PID?

marked as duplicate by Gilles bash Apr 20 '16 at 20:46

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • The pgrep is being substituted before ssh or lsof gets ahold of it. Are you sure that the 2nd ssh is working? Are you ssh'ing from i01n10 to itself? – Jeff Schaller Apr 20 '16 at 2:37
  • @JeffSchaller Hi, JeffSchaller. I am ssh from other node. If "pgrep is being substituted before ssh or lsof gets ahold of it", what should I do – user15964 Apr 20 '16 at 2:59
1

The lsof command can't see your PID because of shell expansion. That means $(pgrep -nf a.out) will be executed on your local server, not remote.

To avoid this expansion, use single quote instead of double quote.

Simple example:

$ foo=local
$ ssh debian8 "foo=remote; echo $foo"
local
$ ssh debian8 'foo=remote; echo $foo'
remote

You might have problem with your pgrep command. This is my simple test using -of instead of -nf flags (Use -af flag to see full command):

// on remote server
# sleep 200 &
[1] 27228
# exit
// on local host 
$ ssh debian8 'echo $(pgrep -nf sleep)'
27244 <-- not expected pid
$ ssh debian8 'echo $(pgrep -of sleep)'
27228 <-- this one

This $() actually launches a subshell, pgrep doesn't report itself as a match but it does report its parent shell. Hence, using -n option will not give you actual pid but the pid of pgrep itself.

  • Hi, cuongnv. I tried your method. ssh i01n10 '/usr/sbin/lsof -p $(pgrep -nf a.out)', however, it gives me nothing – user15964 Apr 20 '16 at 4:24
  • What is that a.out? process name, file name, argument of command used to create the process,...? Try set -x /usr/sbin/lsof -p $(pgrep -nf a.out) to see what happened. Also, try $(pgrep -of a.out) – cuongnv23 Apr 20 '16 at 4:29
  • a.out is the exectuable. I just tried ssh i01n10 'pgrep -nf a.out | xargs /usr/sbin/lsof -p', though it gives output now, but the results is wrong. It should give the path where the running a.out is located. – user15964 Apr 20 '16 at 4:35
  • actually, if I grep txt, it gives /bin/bash, while it should be the path to a.out – user15964 Apr 20 '16 at 4:37
  • how was that a.out executed? can you give the actual output of ps aux | grep a.out? – cuongnv23 Apr 20 '16 at 5:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.