2014-02-22 74 views
4

我無法直接訪問目標主機,需要ssh作爲代理。用ssh殺死遠程進程

如何從本地使用ssh殺死一個進程?我試試這個:

ssh [email protected]$center "ssh [email protected]$ip \"ps -ef|grep -v grep|grep $target_dir/$main|awk '{print \$2}'|xargs kill\"" 

它得到了錯誤:

kill: can't find process "root" 

,以及如何避免錯誤的過程中不存在?

+0

有什麼錯,你已經嘗試了什麼?你是否遇到錯誤? – slayedbylucifer

回答

11

假設你的進程名是name,那麼你可以嘗試這樣的事:

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

謝謝,你的答案爲我工作。只是要指出,在行結尾處有一個錯字,你不需要去看最後的'''字符,所以把「... kill」替換爲「... kill」對我來說是這樣的。 – rakemous