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

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

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

但是,我也经常想将 UID/GID 等显示为输出的一部分。是否可以使用例如将输出列附加ps -o到输出列集ps -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).

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

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.

答案1

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

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

$ ps -o uid_hack,uid,gid,pid,ppid,c,stime,tname,time,cmd

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

$ ps -o Std_f,uid,gid

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

$ ps -C sleep -o uid_hack,uid,gid,pid,ppid,c,stime,tname,time,cmd

相关内容