是否可以将`-o` 选项附加到`ps -Af` 以显示例如UID/GID 作为输出的一部分?

Shu*_*eng 2 process debian ps

我觉得ps -f非常有用,因为它提供了一组强大的默认输出列。

但是,我也经常想在输出中显示 UID/GID 等。是否可以使用例如将输出列附加到输出列ps -ops -f

parallels@debian-gnu-linux-vm:~$ ps -Af -o gid,uid | grep sleep
error: conflicting format options

Usage:
 ps [options]

 Try 'ps --help <simple|list|output|threads|misc|all>'
  or 'ps --help <s|l|o|t|m|a>'
 for additional help text.

For more details see ps(1).
Run Code Online (Sandbox Code Playgroud)

此外,是否有任何grep内置ps,以便我在搜索命令时不会错过列标题?

man ps

-o format
              User-defined format.  format is a single argument in the form of a blank-separated or comma-separated list, which
              offers a way to specify individual output columns.  The recognized keywords are described in the STANDARD FORMAT
              SPECIFIERS section below.  Headers may be renamed (ps -o pid,ruser=RealUser -o comm=Command) as desired.  If all
              column headers are empty (ps -o pid= -o comm=) then the header line will not be output.  Column width will increase as
              needed for wide headers; this may be used to widen up columns such as WCHAN (ps -o pid,wchan=WIDE-WCHAN-COLUMN -o
              comm).  Explicit width control (ps opid,wchan:42,cmd) is offered too.  The behavior of ps -o pid=X,comm=Y varies with
              personality; output may be one column named "X,comm=Y" or two columns named "X" and "Y".  Use multiple -o options when
              in doubt.  Use the PS_FORMAT environment variable to specify a default as desired; DefSysV and DefBSD are macros that
              may be used to choose the default UNIX or BSD columns.
Run Code Online (Sandbox Code Playgroud)

Ste*_*itt 5

您可以使用该-O选项将列添加到默认选择,但它不匹配-fps -O uid,gid显示 pid、uid、gid、状态、tty、时间和命令。(联机帮助页提到了“无状态”变体,我认为它应该对应于“System V”v.“BSD”模式,但的列定义-O不支持这一点。)

如果您想要一组特定的列,则需要完全指定它们;使用procps-ng's ps,相当于-f( -o uid_hack,pid,ppid,c,stime,tname,time,cmd),在uid_hack列后添加 uid 和 gid ,是

$ ps -o uid_hack,uid,gid,pid,ppid,c,stime,tname,time,cmd
Run Code Online (Sandbox Code Playgroud)

procps-ng还支持“宏”;它的联机帮助页提到了“DefBSD”和“DefSysV”,但还定义了许多其他内容,包括Std_f用于以下内容的设置-f

$ ps -o Std_f,uid,gid
Run Code Online (Sandbox Code Playgroud)

ps可以使用多种标准过滤其输出;要搜索sleep,请使用-C

$ ps -C sleep -o uid_hack,uid,gid,pid,ppid,c,stime,tname,time,cmd
Run Code Online (Sandbox Code Playgroud)