0

The following command runs well when i run it localy on the dsired machine:

app_name=*some_proccess_name*
pid=`pgrep $app_name | tail -n 1`

But when i run it in the following way, from a remote pc using ssh it dosn't work:

pid=$(ssh $USER_NAME@$HOST_NAME "echo `pgrep $app_name | tail -n 1`")

The value of pid afterwards is just blank. I am not sure what's wrong (just to clarify i have tried several processes names that are all running on the target pc- that's not the problem ).

P.S when i run the command without echo, like that, i just get stuck inside the remote pc and have to use exit in order to get unstuck and return to my local pc:

pid=$(ssh tester@mir1 "`pgrep indicator-apple | tail -n 1`")
2
  • When you write, "`pgrep indicator-apple | tail -n 1`" with backticks inside double quotes, your shell will run that command as a subshell and then send the result of that command as an extra argument to ssh. Remove the backticks, they are unnecessary: pid=$(ssh tester@mir1 "pgrep indicator-apple | tail -n 1") Commented May 23, 2017 at 19:09
  • unix.stackexchange.com/questions/66581/… Commented Mar 17, 2022 at 9:43

1 Answer 1

3

Less is more

pid=$(ssh tester@mir1 pgrep indicator-apple \| tail -n 1)
Sign up to request clarification or add additional context in comments.

1 Comment

I'd recommend explicitly quoting the entire thing, not just the pipe character; ssh combines multiple arguments into a single command string in a non-terribly-well-defined way, so you may as well do it yourself. ssh tester@mir1 'pgrep indicator-apple | tail -n 1'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.