ps 究竟是 aux 还是 ef

时间:2022-07-22
本文章向大家介绍ps 究竟是 aux 还是 ef,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1 Overview

相信肯定会有同学有遇到过一个面试题,问:如何查看系统当前所有的进程?

这个答案相信大部分人都知道,当然就是 ps 了。但是,如果你回到 ps aux,如果考官问你是否知道 ps -ef 呢?又或者反过来呢?

本文就这个问题简单解答一下。

2 aux or ef

这种问题要解决,也是非常容易的,当然就是查手册了!man ps

...省略...
To see every process on the system using standard syntax:
   ps -e
   ps -ef
   ps -eF
   ps -ely

To see every process on the system using BSD syntax:
   ps ax
   ps axu
...省略...

很明显,不管是 ps aux 还是 ps -ef,都能打印系统所有的进程,那么局别在哪里呢?其实就是在于打印的格式。

3 stand syntax

举个例子。

# ps -ef | head -n 10
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0  2018 ?        00:02:04 /sbin/init
root          2      0  0  2018 ?        00:00:05 [kthreadd]
root          3      2  0  2018 ?        00:03:03 [ksoftirqd/0]
root          5      2  0  2018 ?        00:00:00 [kworker/0:0H]
root          8      2  0  2018 ?        00:02:15 [migration/0]
root          9      2  0  2018 ?        00:00:00 [rcu_bh]

4 BSD syntax

举个例子。

ps aux | head -n 10
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.0  0.0  22388  1620 ?        Ss    2018   2:04 /sbin/init
root          2  0.0  0.0      0     0 ?        S     2018   0:05 [kthreadd]
root          3  0.0  0.0      0     0 ?        S     2018   3:03 [ksoftirqd/0]
root          5  0.0  0.0      0     0 ?        S<    2018   0:00 [kworker/0:0H]
root          8  0.0  0.0      0     0 ?        S     2018   2:15 [migration/0]
root          9  0.0  0.0      0     0 ?        S     2018   0:00 [rcu_bh]
root         10  0.0  0.0      0     0 ?        S     2018   0:00 [rcuob/0]
root         11  0.0  0.0      0     0 ?        S     2018   0:00 [rcuob/1]
root         12  0.0  0.0      0     0 ?        S     2018   0:00 [rcuob/2]A

5 Summary

https://askubuntu.com/questions/129962/ps-ef-vs-ps-aux

从以上那个回答来看 ps -eps ax 是几乎相同的,但是我们从手册可以看到,-fx 选项会有一些局别,如下。

...省略...
      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.
...省略...
       -f              does 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. 

根据手册来看,x 就是打印 BSD 风格,-f 同样也是打印格式的问题。

总体来说,如果只是需要 grep 或者拉进程号之列的操作,以上区别是没有实际意义的,用户可以忽略。