4

I know that I can run command remotely from another machine using ssh -t login@machine "command", however I am struggling to run more complex command like this:

 watch  "ps aux  | awk '{print $1}' | grep php-fpm | wc -l"

I am trying with different kind of quotes however although watch command seems to be firing, it's showing errors like:

awk: cmd. line:1: {print awk: cmd. line:1:
^ unexpected newline or end
of string

0

1 Answer 1

4

The thing is the $ is expanded by the shell before it is passed to the ssh command. You need to deprive it of its special meaning locally by escaping it before passing it to ssh as

ssh -t login@machine watch "ps aux | awk '{print \$1}' | grep php-fpm | wc -l"

The error you are seeing is because when shell tries to expand $1 it does not find a value for it and leaves an empty string which results in incorrect number of arguments passed to awk.

Also you could replace your shell pipeline containing awk and grep with just a simple logic

ssh -t login@machine watch "ps aux | awk '\$1 == \"php-fpm\"{count++}END{print count}'
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.