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.
$ ssh debian8 "foo=remote; echo $foo"
$ ssh debian8 'foo=remote; echo $foo'
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 &
// on local host
$ ssh debian8 'echo $(pgrep -nf sleep)'
27244 <-- not expected pid
$ ssh debian8 'echo $(pgrep -of sleep)'
27228 <-- this one
$() 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