Kill the remote ssh process

I can not directly access the target host, I need ssh as a proxy.

How can I kill a process from local use of ssh? I try this:

ssh root@$center "ssh root@$ip \"ps -ef|grep -v grep|grep $target_dir/$main|awk '{print \$2}'|xargs kill\""

he got an error:

kill: can't find process "root"

And how to avoid an error when the process does not exist?

+4
source share
2 answers

Use pkill -fto easily kill a process by mapping it to the command line.

ssh root@$center ssh root@$ip pkill -f "$target_dir/$main"
+4
source

Suppose your process name name, then you can try something like this:

ssh hostname "pid=\$(ps aux | grep 'name' | awk '{print \$2}' | head -1); echo \$pid |xargs kill"
+11
source

Source: https://habr.com/ru/post/1528236/


All Articles