Linux-ps命令的使用
ps
report a snapshot of the current processes.
统计当前进程运行情况
概述
ps
命令的选项有三种风格
This version of ps accepts several kinds of options:
1 UNIX options, which may be grouped and must be preceded by a dash.
2 BSD options, which may be grouped and must not be used with a dash.
3 GNU long options, which are preceded by two dashes.
UNIX:可以分组,使用一个破折号(dash)开头
1
$ ps -ef
BSD:可以分组,不使用破折号(dash)
1
$ ps aux
GNU:不可以分组,使用两个破折号(dash)开头
1
$ ps --user
ps -aux
和ps aux
是不一样的
Note that “ps -aux” is distinct from “ps aux”. The POSIX and UNIX standards require that “ps -aux” print all processes owned by a
user named “x”, as well as printing all processes that would be selected by the -a option. If the user named “x” does not exist, this
ps may interpret the command as “ps aux” instead and print a warning. This behavior is intended to aid in transitioning old scripts
and habits. It is fragile, subject to change, and thus should not be relied upon.
man
手册中明确提到ps -aux
和ps aux
是完全不同的
在POSIX
和UNIX
标准中ps -aux
会展示归属于用户x
的所有的进程,只有在没有用户名为x
的用户时,ps -aux
被解释为ps aux
,此时这两个命令才是等效的
常用选项
-a
Select all processes except both session leaders (see getsid(2)) and processes not associated with a terminal.
不是全部进程,排除了session leaders
和那些没有关联到终端的进程
没有关联到终端的进程可以简单理解(不完全正确)为后台进程,不由具体的某个终端前台运行
1 | [root@crayon tmp]# ps -a |
TTY
这行都有值,代表这些都是由具体某个终端
关于Linux会话、终端与进程组可以参考
Linux会话、终端与进程组 - 知乎 (zhihu.com)
-u
/U
/--user
-u userlist
Select by effective user ID (EUID) or name. This selects the processes whose effective user name or ID is in userlist. The effective user ID describes the user whose file access permissions are used by the process (see geteuid(2)). Identical to
U and –user.
选择归属于指定用户id或用户名的进程
a
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.
可以列出不属于当前用户的进程信息(解除只列出归属于当前用户的进程的限制)
但是只能列出与终端关联的进程,也就是后台进程不会显示(需要使用x
/-x
选项)
注意man
手册中的最后一句
a
和x
/-x
选项组合使用即可打印所有进程的信息,和-e
选项效果一样
x
/-x
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.
可以列出非终端关联的进程信息(解除只列出与终端关联的进程的限制)
但是只能列出归属于当前用户的进程,也就是其他用户的进程不会显示(需要使用a
选项)
-e
/-A
-e Select all processes. Identical to -A.
-A Select all processes. Identical to -e.
列出所有进程信息
u
u Display user-oriented format.
man
手册的原文解释是面向用户的格式化输出
1 | [root@crayon tmp]# ps u |
除了USER
、PID
外还包含了进程资源占用的信息
-f
/-F
-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 Extra full format. See the -f option, which -F implies.
man
手册的原文解释是完整格式输出
搭配-L
选项还可以输出线程信息:NLWP
线程数和LWP
线程ID
-L
-L Show threads, possibly with LWP and NLWP columns.
显示线程信息
NLWP
:线程数
LWP
:线程ID
f
/--forest
f ASCII art process hierarchy (forest).
–forest
ASCII art process tree.
树形结构输出,这个可以很方便的查看进程层级(父子关系)
和-H
的区别就是-H
只是按照空行缩进
而f
/--forest
会填充一些字符让结果更好看一点
1 | [root@crayon tmp]# ps -ef --forest | head -n 5 |
-H
-H Show process hierarchy (forest).
1 | [root@crayon tmp]# ps -efH |
常用组合
ps -ef f
1 | [root@crayon tmp]# ps -ef f |
按照进程层级以树形打印出所有进程信息
ps axuf
1 | [root@crayon tmp]# ps axu |
按照进程层级以树形打印出所有进程的信息
ax
选项和-e
/-A
等效,都是列出所有的进程
u
选项面向用户打印出关键信息
u
和-f
选项都是控制输出信息,会有冲突,按需选择互斥使用即可
1 | [root@crayon tmp]# ps u -f |
u
选项会输出进程资源的使用情况
参考资料
还有更多的使用细节可以参阅以下文章