通过ssh杀死远程进程

sha*_*ind 6 linux ssh shell

我无法直接访问目标主机,需要ssh作为代理.

如何从本地使用ssh杀死进程?我试试这个:

ssh root@$center "ssh root@$ip \"ps -ef|grep -v grep|grep $target_dir/$main|awk '{print \$2}'|xargs kill\""
Run Code Online (Sandbox Code Playgroud)

它有错误:

kill: can't find process "root"
Run Code Online (Sandbox Code Playgroud)

如何避免过程不存在的错误?

Jay*_*hoi 17

假设您的进程名称是name,那么您可以尝试这样的事情:

ssh hostname "pid=\$(ps aux | grep 'name' | awk '{print \$2}' | head -1); echo \$pid |xargs kill"
Run Code Online (Sandbox Code Playgroud)


Joh*_*ica 4

用于pkill -f通过匹配命令行轻松终止进程。

ssh root@$center ssh root@$ip pkill -f "$target_dir/$main"
Run Code Online (Sandbox Code Playgroud)