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

2024-10-11 08:36:00
admin
原创
218
摘要:问题描述:当我检查进程列表并“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,则可能有所不同。

相关推荐
  政府信创国产化的10大政策解读一、信创国产化的背景与意义信创国产化,即信息技术应用创新国产化,是当前中国信息技术领域的一个重要发展方向。其核心在于通过自主研发和创新,实现信息技术应用的自主可控,减少对外部技术的依赖,并规避潜在的技术制裁和风险。随着全球信息技术竞争的加剧,以及某些国家对中国在科技领域的打压,信创国产化显...
工程项目管理   2525  
  为什么项目管理通常仍然耗时且低效?您是否还在反复更新电子表格、淹没在便利贴中并参加每周更新会议?这确实是耗费时间和精力。借助软件工具的帮助,您可以一目了然地全面了解您的项目。如今,国内外有足够多优秀的项目管理软件可以帮助您掌控每个项目。什么是项目管理软件?项目管理软件是广泛行业用于项目规划、资源分配和调度的软件。它使项...
项目管理软件   1542  
  本文介绍了以下10款项目管理软件工具:禅道项目管理软件、Filestage、Xebrio、Kintone、Monday、Celoxis、Backlog、BubblePPM、Plutio、Scoro。在当今快速变化的商业环境中,项目管理系统的选择成为企业成功的关键因素之一。许多企业在选购项目管理系统时,常常面临功能复杂、...
项目管理系统   0  
  本文介绍了以下10款项目管理软件工具:禅道项目管理软件、Plutio、Kantata、Kintone、LiquidPlanner、Motion、Smartsheet、Targa、QuickBase、Hive。在当今快节奏的商业环境中,项目管理软件已成为企业提升效率、优化资源分配和确保项目按时交付的关键工具。然而,面对市...
开源项目管理软件   15  
  本文介绍了以下10款项目管理软件工具:禅道项目管理软件、Scoro、Wekan、Hubstaff、Kintone、Wrike、Filestage、Trello、Airtable、Paymo。在当今快节奏的商业环境中,项目管理已成为企业成功的关键因素之一。然而,许多项目经理和团队在管理复杂项目时常常面临诸多挑战,如任务分...
项目管理系统   7  
热门文章
项目管理软件有哪些?
曾咪二维码

扫码咨询,免费领取项目管理大礼包!

云禅道AD
禅道项目管理软件

云端的项目管理软件

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

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

内置subversion和git源码管理

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

免费试用