如何让 pgrep 显示完整的进程信息

Joe*_*Fan 32 linux bash grep process

有什么办法可以pgrep让我知道每个进程的所有信息ps吗?我知道我可以ps通过管道,grep但这需要大量打字,而且它还为我提供了grep我不想要的流程本身。

Dan*_*ley 26

pgrep的输出选项非常有限。您几乎肯定需要将其发回ps以获取重要信息。您可以通过在 .bash 文件中使用 bash 函数来自动执行此操作~/.bashrc

function ppgrep() { pgrep "$@" | xargs --no-run-if-empty ps fp; }
Run Code Online (Sandbox Code Playgroud)

然后调用命令。

ppgrep <pattern>
Run Code Online (Sandbox Code Playgroud)

  • 谢谢!我将其修改为:`function ppgrep() { pgrep "$@" | xargs ps fp 2&gt; /dev/null; 否则,如果没有进程匹配您的搜索,它会转储整个 `ps` 用法 megilla。 (3认同)
  • 如果你想避开 ps 使用页面,GNU xargs 有一个选项,`-r`,它只会在收到列表时才执行命令。 (2认同)
  • 更简洁的方法是`ps fp $(pgrep -d, "$@")` (2认同)

nal*_*ply 18

结合pgrepps使用xargs

pgrep <your pgrep-criteria> | xargs ps <your ps options> -p
Run Code Online (Sandbox Code Playgroud)

例如尝试

pgrep -u user | xargs ps -f -p
Run Code Online (Sandbox Code Playgroud)

获取完整的进程列表user。选项-u user限制pgrep给给定的用户(作为数字或名称),而ps选项-f -p请求所选 PID 的完整格式列表。

最好将第一行保留为列名。grep总是删除列名。

  • 这应该是公认的答案,因为它以正确的方式使用 UNIX 管道,从一个工具获取 PID 列表并反馈到另一个工具中(如果这看起来像黑客行为,其实不是——这种技术可以在很多 UNIX 工具中使用) ,就像电子邮件 grep 工具一样。Bash 函数 ppgrep() 是不必要的依赖项,并且避免面对此处提供的学习机会。) (2认同)

Ben*_*kin 12

下面只给你 PID + 完整的命令行。对于“所有信息ps都可以”,请参阅其他答案...

大多数 linux 使用procps-ng。从 3.3.4(2012 年发布)开始,pgrep -a( --list-full) 显示完整的命令行。
注意:默认情况下 pgrep 仅匹配您针对可执行文件名称提供的模式。如果要匹配完整的命令行(如 grepping ps 所做的那样),请添加-f( --full) 选项。

在旧版本(包括原始procps项目)中,-l选项显示信息,但其行为有所不同:

不确定 *BSD 使用什么代码,但他们的手册页记录了旧-fl行为。

不幸的是,你甚至不能-fl便携地使用——在最近的 procps-ng 中,-f( --list-name) 总是只打印可执行文件的名称。


ccp*_*zza 8

Linux

对于 GNU 版本的pgreplong + 模糊输出是通过使用实现的,-af并且字符串必须区分大小写(即没有选项不区分大小写)。

$ pgrep -af apache

OUTPUT:
    1748 /usr/sbin/apache2 -k start
Run Code Online (Sandbox Code Playgroud)

手册页

   -a, --list-full
       List  the  full  command line as well as the process ID.  (pgrep only.)

   -f, --full
       The pattern is normally only matched against the process name.  
       When -f is set, the full command  line is used.
Run Code Online (Sandbox Code Playgroud)

苹果系统

在 OSX 上(以及通过推断,在 BSD 上)-l长输出)与-f匹配完整参数列表)将显示完整的命令(-i添加不区分大小写):

$ pgrep -af apache

OUTPUT:
    1748 /usr/sbin/apache2 -k start
Run Code Online (Sandbox Code Playgroud)

手册页

   -a, --list-full
       List  the  full  command line as well as the process ID.  (pgrep only.)

   -f, --full
       The pattern is normally only matched against the process name.  
       When -f is set, the full command  line is used.
Run Code Online (Sandbox Code Playgroud)