我正在尝试更深入地了解ps
Linux中的命令。
当我使用时我很困惑ps axjf
,ps -axjf
当我发现它们在手册中似乎有不同的含义但它们的输出几乎相同时。
它们之间的具体区别是什么?
对于这些选项,手册说:
a Lift the BSD-style "only yourself" restriction, which is imposed upon the set of all processes when some BSD-style (without "-") options are used or when the ps personality setting is BSD-like. The set of processes selected in this manner is in addition to the set of processes selected by other means. An alternate description is that this option causes ps to list all processes with a terminal (tty), or to list all processes when used together with the x option.
-a Select all processes except both session leaders (see getsid(2)) and processes not associated with a terminal.
x Lift the BSD-style "must have a tty" restriction, which is imposed upon the set of all processes when some BSD-style (without "-") options are used or when the ps personality setting is BSD-like. The set of processes selected in this manner is in addition to the set of processes selected by other means. An alternate description is that this option causes ps to list all processes owned by you (same EUID as ps), or to list all processes when used together with the a option.
j BSD job control format.
-j Jobs format.
f ASCII art process hierarchy (forest).
-f Do 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.
尤其是f
and-f
选项,似乎-f
没有输出层次关系的意义。
我比较了这两个命令的输出,除了几行之外,它们几乎相同。
lightxy233@iZ8vb0rgcfh3yx60rzee1jZ:~$ ls
_axjf.txt axjf.txt
lightxy233@iZ8vb0rgcfh3yx60rzee1jZ:~$ diff _axjf.txt axjf.txt
81c81
< 2 639305 0 0 ? -1 I 0 0:00 \_ [kworker/u4:0-flush-252:0]
---
> 2 639305 0 0 ? -1 I 0 0:00 \_ [kworker/u4:0-events_unbound]
126,127c126,127
< 630827 630830 630830 630830 pts/0 641497 Ss 1000 0:00 | \_ -bash
< 630830 641497 641497 630830 pts/0 641497 R+ 1000 0:00 | | \_ ps axjf
---
> 630827 630830 630830 630830 pts/0 641448 Ss 1000 0:00 | \_ -bash
> 630830 641448 641448 630830 pts/0 641448 R+ 1000 0:00 | | \_ ps -axjf
129c129
< 630845 641496 630845 630845 ? -1 S 1000 0:00 | \_ sleep 1
---
> 630845 641447 630845 630845 ? -1 S 1000 0:00 | \_ sleep 1
我认为这可能涉及混合不同风格的 ps 命令,但我还没有找到很好的解释。
答案1
假设你正在使用procps-ng 的ps
,这确实归结为混合不同的风格,以及如何ps
解释选项。命令中的关键元素是x
选项的使用:因为它仅存在于 BSD 风格中,并且在命令中是明确的(因为-a
不带参数),所以整套选项都以 BSD 风格进行解释。结果,启用相同的选项集,并且输出的差异是由进程的差异引起的axjf
。-axjf
如果删除x
,您将看到差异:ps ajf
和ps -ajf
。
x
手册页中显示的歧义ps
涉及选项的使用-u
:如果以 Unix 风格解释,则需要一个参数来指定用户。因此-ux
(及其变体,例如-aux
)被解释为-u x
,除非没有x
用户,在这种情况下 procps-ngps
应用启发式告诉它切换到 BSD 风格。