0

The follow command works in a local terminal:

ps aux | grep "script.py" | awk {'print $2'} | xargs kill -2

But this command doesn't work remotely:

ssh -o ConnectTimeout=6 john@remote-pc ps aux | grep "script.py" | awk {'print $2'} | xargs kill -2

...even though script.py on remote belongs to john, the user we are ssh as. I can ping successfully and other commands are successful.

JSStuball
  • 2,247
  • 1
  • 19
  • 43

1 Answers1

1

You need to escape the pipes to cause them to be passed to the ssh command rather than being executed by your local shell otherwise everything after ps aux is executed on your local machine.

ssh -o ConnectTimeout=6 john@remote-pc ps aux \| grep "script.py" \| awk {\'print $2\'} \| xargs kill -2
Alan Birtles
  • 22,711
  • 4
  • 22
  • 44
  • Thanks that's great to know. In fact I discovered it was also necessary to escape the single quotes in the `awk` command when executed remotely. – JSStuball Aug 03 '18 at 09:01
  • @JSStuball yep, getting this right is tricky, for anything other than simple commands I tend to write a shell script instead: https://stackoverflow.com/questions/4412238/what-is-the-cleanest-way-to-ssh-and-run-multiple-commands-in-bash – Alan Birtles Aug 03 '18 at 09:10