Linux常用命令-ps

命令

ps

描述

report a snapshot of the current processes
查看所有系统进程

用法

1
ps [ options ]

选项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
ps支持三种格式的选项
UNIX options: 必须加破折号-,如ps -ef
BSD options: 不能加破折号,如ps ef
GNU long options: 加两个破折号--,如ps --version

Options:
a 配合x使用显示所有进程
-A,-e 显示所有进程,等同于-e
-a 显示当前终端的进程
T 显示当前终端的所有进程
r 显示当前终端正在运行的进程
x 配合a使用显示所有进程
-C 指定命令名称,注意为严格匹配
-G 指定GID或组名
q,-q 指定PID
t,-t 指定tty
-U,-u 指定UID或用户名
-f,-F 显示完整格式
j,-j 显示BSD任务格式
l,-l 显示BSD长格式,-l通常配合-y一起使用
-O,-o 指定显示格式
u 显示CPU和内存信息
v 显示内存信息
X 显示ESP和EIP信息
e 显示env环境变量
f 显示进程层级关系
h,-h 不显示标题头部
k 指定排序列
w,-w 不折行输出
m,-m 显示线程
L 显示格式说明

注意

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
$ ps a
PID TTY STAT TIME COMMAND
940 tty1 Ss+ 0:00 /sbin/agetty --noclear tty1 linux
2683 pts/0 Ss+ 0:00 -bash
2713 pts/1 Ss 0:00 -bash
3316 pts/1 R+ 0:00 ps a
# ax显示所有进程
$ ps ax|head
PID TTY STAT TIME COMMAND
1 ? Ss 0:00 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
2 ? S 0:00 [kthreadd]
4 ? S< 0:00 [kworker/0:0H]
5 ? S 0:00 [kworker/u256:0]
6 ? S 0:00 [ksoftirqd/0]
7 ? S 0:00 [migration/0]
8 ? S 0:00 [rcu_bh]
9 ? S 0:00 [rcu_sched]
10 ? S< 0:00 [lru-add-drain]
$ ps -A|head
PID TTY TIME CMD
1 ? 00:00:00 systemd
2 ? 00:00:00 kthreadd
4 ? 00:00:00 kworker/0:0H
5 ? 00:00:00 kworker/u256:0
6 ? 00:00:00 ksoftirqd/0
7 ? 00:00:00 migration/0
8 ? 00:00:00 rcu_bh
9 ? 00:00:00 rcu_sched
10 ? 00:00:00 lru-add-drain
# -a显示当前终端的进程
$ ps -a|head
PID TTY TIME CMD
3336 pts/1 00:00:00 ps
3337 pts/1 00:00:00 head
# T显示当前终端的所有进程
$ ps T|head
PID TTY STAT TIME COMMAND
2713 pts/1 Ss 0:00 -bash
3343 pts/1 R+ 0:00 ps T
3344 pts/1 S+ 0:00 head
# r显示当前终端处于运行状态的进程
$ ps r|head
PID TTY STAT TIME COMMAND
3350 pts/1 R+ 0:00 ps r
# -C过滤命令,注意需要严格匹配命令名称
$ ps -C chrony
PID TTY TIME CMD
$ ps -C chronyd
PID TTY TIME CMD
663 ? 00:00:00 chronyd
# -G指定GID或组名
$ id chrony
uid=998(chrony) gid=996(chrony) groups=996(chrony)
$ ps -G 996
PID TTY TIME CMD
663 ? 00:00:00 chronyd
$ ps -G chrony
PID TTY TIME CMD
663 ? 00:00:00 chronyd
# q或-q指定PID,多个值用逗号分隔
$ ps q 663
PID TTY STAT TIME COMMAND
663 ? S 0:00 /usr/sbin/chronyd
$ ps -q 663
PID TTY TIME CMD
663 ? 00:00:00 chronyd
$ ps p 663,664
PID TTY STAT TIME COMMAND
663 ? S 0:00 /usr/sbin/chronyd
664 ? Ssl 0:00 /usr/sbin/NetworkManager --no-daemon
# t或-t指定终端tty
$ ps t pts/1
PID TTY STAT TIME COMMAND
2713 pts/1 Ss 0:00 -bash
3632 pts/1 R+ 0:00 ps t pts/1
$ ps -t pts/1
PID TTY TIME CMD
2713 pts/1 00:00:00 bash
3626 pts/1 00:00:00 ps
$ ps U chrony
PID TTY STAT TIME COMMAND
663 ? S 0:00 /usr/sbin/chronyd
$ ps -U chrony
PID TTY TIME CMD
663 ? 00:00:00 chronyd
$ ps -u chrony
PID TTY TIME CMD
663 ? 00:00:00 chronyd
$ ps --user chrony
PID TTY TIME CMD
663 ? 00:00:00 chronyd
$ ps -u 998
PID TTY TIME CMD
663 ? 00:00:00 chronyd
$ ps -U 998
PID TTY TIME CMD
663 ? 00:00:00 chronyd

$ ps -c
PID CLS PRI TTY TIME CMD
2713 TS 19 pts/1 00:00:00 bash
5946 TS 19 pts/1 00:00:00 ps
# -f或-F显示完整格式
$ ps -f
UID PID PPID C STIME TTY TIME CMD
root 2713 2681 0 15:03 pts/1 00:00:00 -bash
root 6026 2713 0 16:06 pts/1 00:00:00 ps -f
$ ps -F
UID PID PPID C SZ RSS PSR STIME TTY TIME CMD
root 2713 2681 0 29044 2772 0 15:03 pts/1 00:00:00 -bash
root 6032 2713 0 38862 1840 0 16:06 pts/1 00:00:00 ps -F
# j或-j显示BSD格式
$ ps j
PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND
1 940 940 940 tty1 940 Ss+ 0 0:00 /sbin/agetty --noclear tty1 linux
2681 2683 2683 2683 pts/0 2683 Ss+ 0 0:00 -bash
2681 2713 2713 2713 pts/1 6050 Ss 0 0:00 -bash
2713 6050 6050 2713 pts/1 6050 R+ 0 0:00 ps j
$ ps -j
PID PGID SID TTY TIME CMD
2713 2713 2713 pts/1 00:00:00 bash
6056 6056 2713 pts/1 00:00:00 ps
# l或-ly显示BSD长格式
$ ps l
F UID PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME COMMAND
4 0 940 1 20 0 110204 848 n_tty_ Ss+ tty1 0:00 /sbin/agetty --noclear tty1 linu
4 0 2683 2681 20 0 116072 2532 n_tty_ Ss+ pts/0 0:00 -bash
4 0 2713 2681 20 0 116176 2772 do_wai Ss pts/1 0:00 -bash
0 0 6062 2713 20 0 153324 1500 - R+ pts/1 0:00 ps l
$ ps -l
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
4 S 0 2713 2681 0 80 0 - 29044 do_wai pts/1 00:00:00 bash
0 R 0 6068 2713 0 80 0 - 38331 - pts/1 00:00:00 ps
$ ps -ly
S UID PID PPID C PRI NI RSS SZ WCHAN TTY TIME CMD
S 0 2713 2681 0 80 0 2772 29044 do_wai pts/1 00:00:00 bash
R 0 6074 2713 0 80 0 1504 38331 - pts/1 00:00:00 ps
# -o指定显示格式
$ ps -o user,pid,cmd
USER PID CMD
root 2713 -bash
root 6410 ps -o user,pid,cmd
$ ps -o user= -o pid= -o cmd=
root 2713 -bash
root 6416 ps -o user= -o pid= -o cmd=
# u显示CPU和内存信息
$ ps u
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 940 0.0 0.0 110204 848 tty1 Ss+ 15:03 0:00 /sbin/agetty --noclear tty1 linux
root 2683 0.0 0.1 116072 2532 pts/0 Ss+ 15:04 0:00 -bash
root 2713 0.0 0.1 116176 2772 pts/1 Ss 15:04 0:00 -bash
root 6535 0.0 0.0 155448 1860 pts/1 R+ 16:22 0:00 ps u
# v显示内存信息
$ ps v
PID TTY STAT TIME MAJFL TRS DRS RSS %MEM COMMAND
940 tty1 Ss+ 0:00 1 41 110162 848 0.0 /sbin/agetty --noclear tty1 linux
2683 pts/0 Ss+ 0:00 1 885 115186 2532 0.1 -bash
2713 pts/1 Ss 0:00 0 885 115290 2772 0.1 -bash
6563 pts/1 R+ 0:00 0 90 153233 1500 0.0 ps v
# aux显示所有进程信息
$ ps aux |head
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 125392 3848 ? Ss 15:03 0:01 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
root 2 0.0 0.0 0 0 ? S 15:03 0:00 [kthreadd]
root 4 0.0 0.0 0 0 ? S< 15:03 0:00 [kworker/0:0H]
root 5 0.0 0.0 0 0 ? S 15:03 0:00 [kworker/u256:0]
root 6 0.0 0.0 0 0 ? S 15:03 0:00 [ksoftirqd/0]
root 7 0.0 0.0 0 0 ? S 15:03 0:00 [migration/0]
root 8 0.0 0.0 0 0 ? S 15:03 0:00 [rcu_bh]
root 9 0.0 0.0 0 0 ? S 15:03 0:00 [rcu_sched]
root 10 0.0 0.0 0 0 ? S< 15:03 0:00 [lru-add-drain]
# e显示环境变量
$ ps e
PID TTY STAT TIME COMMAND
940 tty1 Ss+ 0:00 /sbin/agetty --noclear tty1 linux LANG= PATH=/usr/local/sbin:/usr/local/bin
2683 pts/0 Ss+ 0:00 -bash USER=root LOGNAME=root HOME=/root PATH=/usr/local/sbin:/usr/local/bin
2713 pts/1 Ss 0:00 -bash USER=root LOGNAME=root HOME=/root PATH=/usr/local/sbin:/usr/local/bin
6746 pts/1 R+ 0:00 ps e AUTOJUMP_ERROR_PATH=/root/.local/share/autojump/errors.log MANPATH=/op
# f显示进程层级关系
$ ps f
PID TTY STAT TIME COMMAND
2713 pts/1 Ss 0:00 -bash
6778 pts/1 R+ 0:00 \_ ps f
2683 pts/0 Ss+ 0:00 -bash
940 tty1 Ss+ 0:00 /sbin/agetty --noclear tty1 linux
# -h不显示标题
$ ps -h
940 tty1 Ss+ 0:00 /sbin/agetty --noclear tty1 linux
2683 pts/0 Ss+ 0:00 -bash
2713 pts/1 Ss 0:00 -bash
6952 pts/1 R+ 0:00 ps -h
# k指定排序列,如下kuser按照user列排序
$ ps auxkuser |head
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
chrony 663 0.0 0.0 117808 1796 ? S 15:04 0:00 /usr/sbin/chronyd
dbus 654 0.0 0.1 58216 2472 ? Ss 15:04 0:00 /usr/bin/dbus-daemon --system --addre
mysql 2646 0.1 12.6 2187256 256752 ? Sl 15:04 0:09 /usr/local/mysql/bin/mysqld --basedir
polkitd 651 0.0 0.5 612232 12120 ? Ssl 15:04 0:00 /usr/lib/polkit-1/polkitd --no-debug
postfix 1363 0.0 0.2 89848 4076 ? S 15:04 0:00 pickup -l -t unix -u
postfix 1364 0.0 0.2 89916 4100 ? S 15:04 0:00 qmgr -l -t unix -u
root 1 0.0 0.1 125392 3848 ? Ss 15:04 0:01 /usr/lib/systemd/systemd --switched-r
root 2 0.0 0.0 0 0 ? S 15:04 0:00 [kthreadd]
root 4 0.0 0.0 0 0 ? S< 15:04 0:00 [kworker/0:0H]
# m或-m显示线程
$ ps m
PID TTY STAT TIME COMMAND
940 tty1 - 0:00 /sbin/agetty --noclear tty1 linux
- - Ss+ 0:00 -
2683 pts/0 - 0:00 -bash
- - Ss+ 0:00 -
2713 pts/1 - 0:00 -bash
- - Ss 0:00 -
7170 pts/1 - 0:00 ps m
- - R+ 0:00 -
$ ps -m
PID TTY TIME CMD
2713 pts/1 00:00:00 bash
- - 00:00:00 -
7176 pts/1 00:00:00 ps
- - 00:00:00 -
# 显示格式说明
$ ps L|head
%cpu %CPU
%mem %MEM
_left LLLLLLLL
_left2 L2L2L2L2
_right RRRRRRRR
_right2 R2R2R2R2
_unlimited U
_unlimited2 U2
alarm ALARM
args COMMAND
# 按user列排序
$ ps -eo pid,user,args --sort user
PID USER COMMAND
663 chrony /usr/sbin/chronyd
654 dbus /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activat
2646 mysql /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-d
651 polkitd /usr/lib/polkit-1/polkitd --no-debug
1364 postfix qmgr -l -t unix -u
6907 postfix pickup -l -t unix -u
1 root /usr/lib/systemd/systemd --switched-root --system --deserialize 22
2 root [kthreadd]
4 root [kworker/0:0H]
5 root [kworker/u256:0]
6 root [ksoftirqd/0]