【问题标题】:kill remote process by ssh [closed]通过ssh杀死远程进程[关闭]
【发布时间】:2014-03-23 21:16:53
【问题描述】:

我不能直接访问目标主机,需要ssh作为代理。

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

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

出现错误:

kill: can't find process "root"

以及如何避免进程不存在的错误?

【问题讨论】:

  • 您的尝试有什么问题?你有错误吗?

标签: linux shell ssh


【解决方案1】:

假设你的进程名称是name,那么你可以试试这样:

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

【讨论】:

  • 谢谢,您的回答对我有用。只是要指出,在行尾有一个错字,您不需要对最后一个 '"' 字符进行转义。因此将“... kill\”替换为“... kill” 对我来说确实如此。
【解决方案2】:

使用pkill -f 通过匹配其命令行轻松杀死进程。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-24
    相关资源
    最近更新 更多