更优雅的“ps aux | grep -v grep”

2024-10-11 08:36:00
admin
原创
187
摘要:问题描述:当我检查进程列表并“grep”出我感兴趣的进程时,进程grep本身也会包含在结果中。例如,要列出终端:$ ps aux | grep terminal user 2064 0.0 0.6 181452 26460 ? Sl Feb13 5:41 gnome-termin...

问题描述:

当我检查进程列表并“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来删除最后的条目...但这并不优雅:)

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


解决方案 1:

通常的技术是这样的:

ps aux | egrep '[t]erminal'

这将匹配包含 的行terminal,但实际上egrep '[t]erminal'不包含!它还适用于多种版本的 Unix。

解决方案 2:

使用pgrep。 它更可靠。

解决方案 3:

这个答案建立在先前的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

与使用pswith进行比较grep。有用的标题行不会被打印:

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

解决方案 4:

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

ps u -C gnome-terminal

(或者使用find等方法搜索 /proc )

解决方案 5:

还有一个选择:

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.

解决方案 6:

免责声明:我是该工具的作者,但是...

我会使用px:

~ $ px atom
  PID COMMAND          USERNAME   CPU RAM COMMANDLINE
14321 crashpad_handler walles   0.01s  0% /Users/walles/Downloads/Atom.app/Contents/Frameworks/Electron Framework.framework/Resources/crashpad_handler --database=
16575 crashpad_handler walles   0.01s  0% /Users/walles/Downloads/Atom.app/Contents/Frameworks/Electron Framework.framework/Resources/crashpad_handler --database=
16573 Atom Helper      walles    0.5s  0% /Users/walles/Downloads/Atom.app/Contents/Frameworks/Atom Helper.app/Contents/MacOS/Atom Helper --type=gpu-process --cha
16569 Atom             walles   2.84s  1% /Users/walles/Downloads/Atom.app/Contents/MacOS/Atom --executed-from=/Users/walles/src/goworkspace/src/github.com/github
16591 Atom Helper      walles   7.96s  2% /Users/walles/Downloads/Atom.app/Contents/Frameworks/Atom Helper.app/Contents/MacOS/Atom Helper --type=renderer --no-san

除了使用合理的命令行界面查找进程之外,它还可以做很多其他有用的事情,更多详细信息请参阅项目页面。

适用于 Linux 和 OS X,可轻松安装:

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

解决方案 7:

使用括号将搜索模式中的字符括起来会排除该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

解决方案 8:

根据最终的用例,您通常希望选择 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朋友已经存在很长时间了,并且比大多数临时重新实现更好地处理整个问题空间。

解决方案 9:

另一个选择是编辑您的.bash_profile(或保存 bash 别名的其他文件)以创建一个从结果中取出“grep”的函数。

function mygrep {
grep -v grep | grep --color=auto $1
}

alias grep='mygrep'

必须grep -v grep是第一位的,否则你--color=auto将因某种原因无法工作。

如果您使用 bash,则此方法有效;如果您使用不同的 shell,则可能有所不同。

相关推荐
  为什么项目管理通常仍然耗时且低效?您是否还在反复更新电子表格、淹没在便利贴中并参加每周更新会议?这确实是耗费时间和精力。借助软件工具的帮助,您可以一目了然地全面了解您的项目。如今,国内外有足够多优秀的项目管理软件可以帮助您掌控每个项目。什么是项目管理软件?项目管理软件是广泛行业用于项目规划、资源分配和调度的软件。它使项...
项目管理软件   1265  
  IPD(Integrated Product Development)即集成产品开发,是一套先进的、成熟的产品开发管理理念、模式和方法。随着市场竞争的日益激烈,企业对于提升产品开发效率、降低成本、提高产品质量的需求愈发迫切,IPD 项目管理咨询市场也迎来了广阔的发展空间。深入探讨 IPD 项目管理咨询的市场需求与发展,...
IPD集成产品开发流程   17  
  IPD(Integrated Product Development)产品开发流程是一套先进的、被广泛应用的产品开发管理体系,它涵盖了从产品概念产生到产品推向市场并持续优化的全过程。通过将市场、研发、生产、销售等多个环节紧密整合,IPD旨在提高产品开发的效率、质量,降低成本,增强企业的市场竞争力。深入了解IPD产品开发...
IPD流程中TR   21  
  IPD(Integrated Product Development)测试流程是确保产品质量、提升研发效率的关键环节。它贯穿于产品从概念到上市的整个生命周期,对企业的成功至关重要。深入理解IPD测试流程的核心要点,有助于企业优化研发过程,打造更具竞争力的产品。以下将详细阐述IPD测试流程的三大核心要点。测试策略规划测试...
华为IPD   18  
  华为作为全球知名的科技企业,其成功背后的管理体系备受关注。IPD(集成产品开发)流程作为华为核心的产品开发管理模式,在创新管理与技术突破方面发挥了至关重要的作用。深入剖析华为 IPD 流程中的创新管理与技术突破,对于众多企业探索自身发展路径具有重要的借鉴意义。IPD 流程概述IPD 流程是一种先进的产品开发管理理念和方...
TR评审   16  
热门文章
项目管理软件有哪些?
云禅道AD
禅道项目管理软件

云端的项目管理软件

尊享禅道项目软件收费版功能

无需维护,随时随地协同办公

内置subversion和git源码管理

每天备份,随时转为私有部署

免费试用