0

I have a script I want to run on remote via ssh. It checks if there is a process running and should try to kill it, if it exists. Now, my code looks like this:

ssh my_prod_env << ENDSSH
...
pid=$(pgrep -f "node my_app.js")
echo $pid
# kill process with $pid
...
exit
ENDSSH

The problem lies here: I cannot capture output of pgrep command in variable. I tried with $(), backticks, pipe then read and maybe other approaches, but all without success.

I would like to do it all in one ssh session.

Now I am thinking the output of command goes to the output stream I cannot access in my script. I might be wrong, though. Either way, help will be appreciated.

2
  • ssh my_prod_env | pgrep -f "node my_app.js" ? Commented Mar 3, 2016 at 10:25
  • I guess it works but can I avoid connecting once again only for this command? I have to stay on remote server to do something else after this command finishes and would like to do it all in one ssh session. Commented Mar 3, 2016 at 10:32

2 Answers 2

0

Ok, after you provided in comments more info what you want, I believe this is the correct answer to your question:

ssh my_prod_env -t 'pgrep -f "node my_app.js"'

This will call the command and leave you logged on the server

Sign up to request clarification or add additional context in comments.

Comments

0

This is what fixes the thing - "escaping" the ENDSSH tag.

ssh my_prod_env << /ENDSSH
...
# capture output of remote commands in remote variables
...
ENDSSH

Problem was that my vars were local and I was trying to capture output of remote commands in them.

This question/answer helped me realize what is going on: How to assign local variable with a remote command result in bash script?

So, my question could be marked as duplicate or something similar, I guess.

Comments

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.