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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9375711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:42:54  来源:igfitidea点击:

More elegant "ps aux | grep -v grep"

linuxgrep

提问by Jakub M.

When I check list of processes and 'grep' out those that are interesting for me, the grepitself is also included in the results. For example, to list terminals:

当我检查进程列表并“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

Normally I use ps aux | grep something | grep -v grepto get rid of the last entry... but it is not elegant:)

通常我ps aux | grep something | grep -v grep用来摆脱最后一个条目......但它并不优雅:)

Do you have a more elegant hack to solve this issue (apart of wrapping all the command into a separate script, which is also not bad)

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

采纳答案by Johnsyweb

The usual technique is this:

通常的技术是这样的:

ps aux | egrep '[t]erminal'

This will match lines containing terminal, which egrep '[t]erminal'does not! It also works on manyflavours of Unix.

这将匹配包含terminal,egrep '[t]erminal'不匹配的行!它也适用于多种Unix。

回答by DarkDust

Use pgrep. It's more reliable.

使用pgrep。它更可靠。

回答by Andreas Frische

You can filter in the ps command, e.g.

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

ps u -C gnome-terminal

(or search through /proc with findetc.)

(或通过 /proc 使用find等进行搜索)

回答by TaXXoR

One more alternative:

一种选择

ps -fC terminal

Here the options:

这里的选项:

 -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.

回答by Acumenus

This answer builds upon a prior pgrepanswer. It also builds upon another answercombining the use of pswith pgrep. Here are some pertinent training examples:

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

$ 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]

The above can be used as a function:

以上可以用作函数

$ 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

Compare with using pswith grep. The useful header row is not printed:

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

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

回答by FlyingCodeMonkey

Another option is to edit your .bash_profile(or other file that you keep bash aliases in) to create a function that greps 'grep' out of the results.

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

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

alias grep='mygrep'

The grep -v grephas to be first otherwise your --color=autowon't work for some reason.

grep -v grep必须是第一,否则你--color=auto不会出于某种原因。

This works if you're using bash; if you're using a different shell YMMV.

如果您使用 bash,这会起作用;如果您使用不同的外壳 YMMV。

回答by auntchilada

Using brackets to surround a character in the search pattern excludes the grepprocess since it doesn't contain the matching regex.

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

回答by Johan Walles

Disclaimer: I'm the author of this tool, but...

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

I'd use px:

我会使用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

Except for finding processes with a sensible command line interface it also does a lot of other useful things, more details on the project page.

除了使用合理的命令行界面查找进程外,它还可以做很多其他有用的事情,更多细节在项目页面上

Works on Linux and OS X, easily installed:

适用于 Linux 和 OS X,易于安装:

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

回答by tripleee

Depending on the ultimate use case, you often want to prefer Awk instead.

根据最终用例,您通常更喜欢 Awk。

ps aux | awk '/[t]erminal/'

This is particularly true when you have something like

当你有类似的东西时尤其如此

ps aux | grep '[t]erminal' | awk '{print }'  # useless use of grep!

where obviously the regex can be factored into the Awk script trivially:

显然,正则表达式可以简单地分解到 awk 脚本中:

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

But really, don't reinvent this yourself. pgrepand friends have been around for a long time and handle this entire problem space much better than most ad hoc reimplementations.

但实际上,不要自己重新发明。pgrep和朋友们已经存在很长时间了,并且比大多数临时重新实现更好地处理整个问题空间。