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
  • 43,482
  • 10
  • 63
  • 98
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
  • 497,756
  • 71
  • 530
  • 681
  • A quoted heredoc is another approach worth showcasing. (`ssh someUser@someHost 'bash -s' <<'EOF'`; `kill $(ps -aux | grep 'screen -S cats' | awk '{print $2}')` on its own line; then `EOF` on a third). – Charles Duffy Jul 30 '19 at 14:47
  • I like to avoid those, as inevitably someone tries to pass a script via standard input that itself tries to read from standard input. – chepner Jul 30 '19 at 14:48
  • Thank u very much guys! – Alina Jul 30 '19 at 14:51