Linux “更优雅”;ps aux“grep-v grep”;

Linux “更优雅”;ps aux“grep-v grep”;,linux,grep,Linux,Grep,当我检查流程列表并“grep”出我感兴趣的流程时,grep本身也包含在结果中。例如,要列出端子,请执行以下操作: $ ps aux | grep terminal user 2064 0.0 0.6 181452 26460 ? Sl Feb13 5:41 gnome-terminal --working-directory=.. user 2979 0.0 0.0 4192 796 pts/3 S+ 11:07 0:00 grep --

当我检查流程列表并“grep”出我感兴趣的流程时,
grep
本身也包含在结果中。例如,要列出端子,请执行以下操作:

$ ps aux  | grep terminal
user  2064  0.0  0.6 181452 26460 ?        Sl   Feb13   5:41 gnome-terminal --working-directory=..
user  2979  0.0  0.0   4192   796 pts/3    S+   11:07   0:00 grep --color=auto terminal
通常我使用
ps aux | grep something | grep-v grep
来删除最后一个条目。。。但它并不优雅:)


您是否有更优雅的技巧来解决此问题(除了将所有命令包装到单独的脚本中,这也不错)

使用。它更可靠。

您可以在ps命令中进行筛选,例如:

ps u -C gnome-terminal

(或使用find等搜索/proc)

通常的技术是:

ps aux | egrep '[t]erminal'
这将匹配包含
终端的行,而
egrep'[t]terminal'
不匹配!它还适用于多种Unix风格。

还有一种:

以下是选项:

 -f        does full-format listing. This option can be combined
           with many other UNIX-style options to add additional
           columns. It also causes the command arguments to be
           printed. When used with -L, the NLWP (number of
           threads) and LWP (thread ID) columns will be added. See
           the c option, the format keyword args, and the format
           keyword comm.

 -C cmdlist     Select by command name.
                This selects the processes whose executable name is
                given in cmdlist.

此答案基于先前的
pgrep
。它还建立在另一个结合使用
ps
pgrep
的基础上。以下是一些相关的培训示例:

$ pgrep -lf sshd
1902 sshd

$ pgrep -f sshd
1902

$ ps up $(pgrep -f sshd)
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root      1902  0.0  0.1  82560  3580 ?        Ss   Oct20   0:00 /usr/sbin/sshd -D

$ ps up $(pgrep -f sshddd)
error: list of process IDs must follow p
[stderr output truncated]

$ ps up $(pgrep -f sshddd) 2>&-
[no output]
上述功能可用作功能

$ psgrep() { ps up $(pgrep -f $@) 2>&-; }

$ psgrep sshd
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root      1902  0.0  0.1  82560  3580 ?        Ss   Oct20   0:00 /usr/sbin/sshd -D
与使用
ps
grep
进行比较。未打印有用的标题行:

$  ps aux | grep [s]shd
root      1902  0.0  0.1  82560  3580 ?        Ss   Oct20   0:00 /usr/sbin/sshd -D

另一个选项是编辑
.bash\u配置文件(或保存bash别名的其他文件),以创建一个从结果中删除“grep”的函数。

必须首先使用
grep-v grep
,否则您的
--color=auto
将因某种原因无法工作


如果您使用的是bash,那么它就可以工作;如果您使用的是不同的shell YMMV。

在搜索模式中使用括号包围字符会排除
grep
过程,因为它不包含匹配的正则表达式

$ ps ax | grep 'syslogd'
   16   ??  Ss     0:09.43 /usr/sbin/syslogd
18108 s001  S+     0:00.00 grep syslogd

$ ps ax | grep '[s]yslogd'
   16   ??  Ss     0:09.43 /usr/sbin/syslogd

$ ps ax | grep '[s]yslogd|grep'
   16   ??  Ss     0:09.43 /usr/sbin/syslogd
18144 s001  S+     0:00.00 grep [s]yslogd|grep

免责声明:我是这个工具的作者,但是

我会使用:

除了使用合理的命令行界面查找进程外,它还做了许多其他有用的事情,包括更多详细信息

适用于Linux和OS X,安装方便:

curl -Ls https://github.com/walles/px/raw/python/install.sh | bash

根据最终的用例,您通常希望选择Awk

ps aux | awk '/[t]erminal/'
尤其是当你有

ps aux | grep '[t]erminal' | awk '{print $1}'  # useless use of grep!
显然,正则表达式可以简单地分解到Awk脚本中:

ps aux | awk '/[t]erminal/ { print $1 }'


但真的,不要自己重新发明这个
pgrep
和朋友们已经存在很长时间了,他们处理整个问题空间的能力比大多数临时重新实施要好得多。

pgrep
如果我查找例如
ps aux | grep'ssh options'
Jakub M.
pgrep-f
怎么办?@Jakub-M默认情况下
pgrep
只匹配与进程名称相对应的模式。要匹配整个命令,请使用
-f
标志。
pgrep
仅返回进程ID。较旧版本使用
pgrep-fl
(但如果没有
-f
匹配完整cmdline,则无法查看完整cmdline,详细信息:)。但如果您需要pid、cmdline之外的任何其他信息,则需要
ps
。可以将它们结合起来:
ps-p$(pgrep-f foo)
请注意,这适用于GNU的ps(Linux),但不适用于BSD ps。这即使适用于GNU ps也是不正确的。
ps-C
将匹配确切的命令。当与
a
x
选项一起使用时,它将报告所有进程,因为
a
x
除了列出通过其他方式匹配的进程集外,还列出了进程。@Animism True。谢谢,我修复了它。这只在进程名被精确指定的情况下有效。它不适用于部分匹配,例如
systemd logind
logind
,也不适用于匹配参数。值得一提的是,这是一个古老的常见问题解答。参见第3.10项,以供参考。这就是他们的方法:
ps-ux | awk'/name/&&/awk/{print$2}'
grep-v grep
部件在做什么?@Jwan622
grep-v grep
从grep结果中排除
grep
。如果grep与ps结合使用,那么grep进程(带有grep参数)也将显示出来,从而使结果变得混乱。grep-v grep是一种常用的避免这种情况的方法一个小缺点,也是一个与OP的问题无关的缺点,就是它不会向你展示像Tomcat这样的东西,它实际上是作为java运行的,有一系列参数。
-C
选项已经在@Andreas Frishe一年半前发布的答案中提出了……这对任意字符串有效,但对用户名无效,例如
ps aux | grep'[r]oot'
。有人知道为什么吗?@kxsong:
|grep'[t]terminal'
选择任何包含单词“terminal”的行,而不将单词“terminal”放入进程列表中。您试图通过
|grep'[r]oot'
实现什么?它是如何不起作用的?可能会有更好的解决方案。如果我错了,请纠正我,但这也适用于格雷普角色的任何位置:ps aux | grep“te[r]minal”brilliant hack(我想我应该这样称呼它,因为aux/grep作者可能没有想到这个场景。)@JamieJag:嗯。。。正如我在帖子中所说,
grep'[t]terminal'
将匹配包含
终端的行。
ps aux
的输出将有一行带有
grep'[t]terminal'
(带方括号),该行不包含字符串
terminal
(没有相同)。请参阅(但它特定于Zsh)。@blueyed,我已经用bash函数定义更新了答案;e、 g.
ps-fp$(pgrep-d,getty)
我使用这个:
ps-uxp`pgrep`
注意,
p
必须是最后一个参数(即,
pux
将不起作用),这不会像
grep
那样对搜索进程名称进行着色,这与别名和函数有什么关系?只需执行
函数grep{command grep-v grep|command grep--color=auto“$@”}
(还要注意参数和引号的修复)。然而,这一点在任何非
ps
调用
grep时被打破<
ps aux | awk '/[t]erminal/'
ps aux | grep '[t]erminal' | awk '{print $1}'  # useless use of grep!
ps aux | awk '/[t]erminal/ { print $1 }'