概述
相信肯定会有同学有遇到过一个面试题,问:如何查看系统当前所有的进程?这个答案相信大部分人都知道,当然就是 ps
了。但是,如果你回到 ps aux
,如果考官问你是否知道 ps -ef
呢?又或者反过来呢?本文就这个问题简单解答一下。
aux还是ef
这种问题要解决,也是非常容易的,当然就是查手册了!man ps
。
1
2
3
4
5
6
7
8
9
10
11
|
...省略...
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
,都能打印系统所有的进程,那么局别在哪里呢?其实就是在于打印的格式。
stand syntax
举个例子
1
2
3
4
5
6
7
8
|
# 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]
|
BSD syntax
举个例子
1
2
3
4
5
6
7
8
9
10
11
|
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
|
总结
从这个回答来看 ps -e
和 ps ax
是几乎相同的,但是我们从手册可以看到,-f
和 x
选项会有一些局别,如下。
1
2
3
4
|
...省略...
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
或者拉进程号之列的操作,以上区别是没有实际意义的,用户可以忽略。
警告
本文最后更新于 2022年5月9日,文中内容可能已过时,请谨慎参考。