1

I want to kill process. Without kill cmd it works fine. But with kill cmd i get:

grep: invalid option -- 'S'
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.
awk: cmd. line:1: {print
awk: cmd. line:1:       ^ unexpected newline or end of string
kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]

Doesn't work:

ssh someUser@someHost kill $(ps -aux | grep 'screen -S cats' | awk '{print $2}')

Works fine:

ssh someUser@someHost ps -aux | grep 'screen -S cats' | awk '{print $2}'
ForceBru
  • 41,233
  • 10
  • 61
  • 89
Alina
  • 27
  • 4

1 Answers1

2

The command substitution runs ps locally, to produce the argument for the remote kill command.

To make the command substitution run remotely, quote the entire string (which is a good idea; you generally don't want ssh having to figure out how to join its arguments into a single command).

# Using $'...', which may contain escaped single quotes
ssh someUser@someHost $'kill $(ps -aux | awk \'/screen -S cats/ {print $2}\''

or

# Using standard double quotes, but making the user responsible
# for escaping any dollar signs that need to be passed literally.
ssh someUser@someHost "kill \$(ps -aux | awk '/screen -S cats/ {print \$2}'"
chepner
  • 446,329
  • 63
  • 468
  • 610