首页 > 解决方案 > 在一行中杀死“ps aux”输出

问题描述

我必须每天多次做同样的事情:

ps aux

寻找运行 ssh 到我的一台服务器的进程...

kill -9 <pid>

我正在寻找是否可以将进程别名为一行。ps aux 的输出通常是这样的:

user   6871   0.0  0.0  4351260      8   ??  Ss    3:28PM   0:05.95 ssh -Nf -L 18881:my-server:18881
user   3018   0.0  0.0  4334292     52   ??  S    12:08PM   0:00.15 /usr/bin/ssh-agent -l
user   9687   0.0  0.0  4294392    928 s002  S+   10:48AM   0:00.00 grep ssh

我总是想用my-server它来终止进程。

有谁知道我怎么能做到这一点?

标签: linuxmacossshcommand-lineps

解决方案


for pid in $(ps aux | grep "[m]y-server" | awk '{print $2}'); do kill -9 $pid; done

ps辅助| grep "[m]y 服务器" | awk '{print $2}' - 这部分为您提供包含“my-server”的 pids 进程列表。这个脚本将遍历这个列表并杀死所有这些进程。


推荐阅读