如何获取带标题的 ps 输出

Sau*_*abh 0 linux debian ps

问题很基本,如何ps在 Linux 中获取带头的输出。

Linux 详细信息:

PRETTY_NAME="Debian GNU/Linux 7 (wheezy)"
NAME="Debian GNU/Linux"
VERSION_ID="7"
VERSION="7 (wheezy)"
Run Code Online (Sandbox Code Playgroud)

我可以用 来获取它ps -ef | { head -1; grep query; },但文字很多。在查看psusing的文档时man ps,我可以看到有一个选项--headers,但使用它也不会给出标题:

>~$ ps aux --headers | grep grep
user  24082  0.0  0.0   6656   628 pts/0    S+   12:59   0:00 grep grep
Run Code Online (Sandbox Code Playgroud)

还尝试h使用命令选项:

~$ ps auxh  | grep grep
user  25982  0.0  0.0   6656   624 pts/0    S+   13:14   0:00 grep grep
Run Code Online (Sandbox Code Playgroud)

Arc*_*mar 5

  1. 我不确定headers是否需要。
  2. 标头被打印,但被 grep 过滤掉。

awk解决方案

ps aux --headers| awk 'NR==1 || /awk/ '
Run Code Online (Sandbox Code Playgroud)
  • NR==1保留第一行
  • ||或者
  • /awk/与 awk 一致

egrep解决方案

ps aux | egrep '^USER|grep' 
Run Code Online (Sandbox Code Playgroud)
  • ^USER行首的 USER字符串
  • grep字符串

解决方案

如果字符串与程序匹配,您可以使用-C选项(以及所需的字段)

ps -C sshd -o stime,etime,args
Run Code Online (Sandbox Code Playgroud)