首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >更优雅的"ps aux | grep -v grep“

更优雅的"ps aux | grep -v grep“
EN

Stack Overflow用户
提问于 2012-02-21 18:15:50
回答 7查看 187K关注 0票数 183

当我检查进程列表并'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去掉最后一个条目...但它并不优雅:)

你有没有一个更优雅的方法来解决这个问题(除了将所有的命令打包到一个单独的脚本中,这也不是很糟糕)

EN

回答 7

Stack Overflow用户

发布于 2012-02-21 18:17:52

使用pgrep。它更可靠。

票数 57
EN

Stack Overflow用户

发布于 2012-02-21 18:22:51

您可以在ps命令中进行过滤,例如

ps u -C gnome-terminal

(或者使用find等在/proc中搜索)

票数 10
EN

Stack Overflow用户

发布于 2013-09-30 15:57:03

再来一个alternative

ps -fC terminal

这里有几个选项:

 -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.
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9375711

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档