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 -auxps 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 -auxps aux是完全不同的

POSIXUNIX标准中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
2
3
4
5
6
[root@crayon tmp]# ps -a
PID TTY TIME CMD
2199 pts/0 00:00:00 tail
2416 pts/2 00:00:00 man
2425 pts/2 00:00:00 less
2906 pts/1 00:00:00 ps

TTY这行都有值,代表这些都是由具体某个终端

关于Linux会话、终端与进程组可以参考

Linux会话、终端与进程组 - 知乎 (zhihu.com)

Linux TTY/PTS概述 - Linux程序员 - SegmentFault 思否

Linux session和进程组概述 - Linux程序员 - SegmentFault 思否

-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手册中的最后一句

ax/-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
2
3
4
5
[root@crayon tmp]# ps u
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 681 0.0 0.0 110204 856 tty1 Ss+ 14:03
...
root 3381 0.0 0.0 155448 1868 pts/1 R+ 20:40 0:00 ps u

除了USERPID外还包含了进程资源占用的信息

-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
2
3
4
5
6
[root@crayon tmp]# ps -ef --forest | head -n 5
UID PID PPID C STIME TTY TIME CMD
root 2 0 0 14:02 ? 00:00:00 [kthreadd]
root 4 2 0 14:02 ? 00:00:00 \_ [kworker/0:0H]
root 5 2 0 14:02 ? 00:00:00 \_ [kworker/u2:0]
root 6 2 0 14:02 ? 00:00:03 \_ [ksoftirqd/0]

-H

-H Show process hierarchy (forest).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@crayon tmp]# ps -efH
root 1088 1 0 14:03 ? 00:00:00 /usr/sbin/sshd -D
root 1787 1088 0 14:59 ? 00:00:02 sshd: root@pts/0
root 1791 1787 0 14:59 pts/0 00:00:00 -bash
root 2199 1791 0 15:11 pts/0 00:00:00 tail -f -n 100 /dev/null
root 2172 1088 0 15:11 ? 00:00:07 sshd: root@pts/1
root 2176 2172 0 15:11 pts/1 00:00:00 -bash
root 3413 2176 0 20:59 pts/1 00:00:00 ps -efH
root 3414 2176 0 20:59 pts/1 00:00:00 grep --color=auto -C 5 1088
root 2295 1088 0 15:30 ? 00:00:06 sshd: root@pts/2
root 2299 2295 0 15:30 pts/2 00:00:00 -bash
root 3097 2299 0 16:52 pts/2 00:00:00 man ps
root 3106 3097 0 16:52 pts/2 00:00:00 less -s
root 2371 1088 0 15:39 ? 00:00:01 sshd: root@pts/3
root 2375 2371 0 15:39 pts/3 00:00:00 -bash
root 1092 1 0 14:03 ? 00:00:07 /usr/sbin/rsyslogd -n

常用组合

ps -ef f

1
2
3
4
5
6
7
[root@crayon tmp]# ps -ef f
UID PID PPID C STIME TTY STAT TIME CMD
root 2 0 0 14:02 ? S 0:00 [kthreadd]
root 4 2 0 14:02 ? S< 0:00 \_ [kworker/0:0H]
root 5 2 0 14:02 ? S 0:00 \_ [kworker/u2:0]
root 6 2 0 14:02 ? S 0:03 \_ [ksoftirqd/0]
...

按照进程层级以树形打印出所有进程信息

ps axuf

1
2
3
4
5
6
[root@crayon tmp]# ps axu
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.3 128056 6728 ? Ss 14:02 0:05 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
root 2 0.0 0.0 0 0 ? S 14:02 0:00 [kthreadd]
root 4 0.0 0.0 0 0 ? S< 14:02 0:00 [kworker/0:0H]
root 5 0.0 0.0 0 0 ? S 14:02 0:00 [kworker/u2:0]

按照进程层级以树形打印出所有进程的信息

ax选项和-e/-A等效,都是列出所有的进程

u选项面向用户打印出关键信息

u-f选项都是控制输出信息,会有冲突,按需选择互斥使用即可

1
2
[root@crayon tmp]# ps u -f
error: conflicting format options

u选项会输出进程资源的使用情况

参考资料

还有更多的使用细节可以参阅以下文章

较为平滑的 Linux ps 命令入门 - 知乎 (zhihu.com)