《Linux》 ps命令
作用:
返回当前进程的一个快照
用法:
ps [ options]
描述:
ps显示有关所选活动进程的信息。如果要重复更新选定内容和显示的信息,请改用top(1)。 ps接受3种options: UNIX options , 可以分组,前面必须加破折号-。 BSD options, 可以分组,不加破折号-。 GNU long options, 需要两个破折号–。 不同类型的选项可以自由混合,但可能会出现冲突。有一些同义的选项,它们在功能上是相同的,因为这个ps兼容许多标准和ps实现。 默认情况下,ps选择所有进程的用户ID和联系的终端 它展示 process ID (pid=PID), the terminal associated with the process (tname=TTY), the cumulated CPU time in [DD-]hh:mm:ss format (time=TIME), and the executable name (ucmd=CMD). BSDoptions将会添加STAT(进程状态)去默认展示中,显示命令args(args=command),而不是可执行文件名。
示例:
简单的进程选择:
options 含义 a 此选项使ps列出带有终端(tty)的所有进程,或者与x选项一起使用时列出所有进程。 -A或-e 选出所有进程 -a 选择除会话引导程序(请参阅getsid(2))和未与终端关联的进程之外的所有进程。 -d 选择除会话引导程序的所有进程 。 –deselect或-N 取反 T 选择与终端关联的所有进程。 r 选择正在运行的进程 x 此选项使ps列出您拥有的所有进程(和ps相同的EUID),或者列出和选项一起使用时的所有进程。
带参数列表的进程选择:
options 含义 -C cmdlist 根据命令名字选择 -G grplist 根据一个GID或组名字选择 -g grplist 只有在同时指定了某些组名时,组ID号才起作用。 –Group grplist 同 -G grplist –group grplist 同 -g grplist p pidlist 根据PID选择 同-p pidlist 和 --pid pidlist –ppid pidlist 根据PPID选择 q pidlist 根据PID(快速模式)选择 同-q pidlist 和 --quick-pid pidlist -s sesslist 选择根据阶段ID 同 --sid sesslist t ttylist 根据tty选择 同-t ttylist 和 --tty ttylist U userlist 根据UID选择 同 -u userlist 和 --User userlist 和 --user userlist
输出格式控制:
ps -o pid,state,time,command
PID S TIME COMMAND
1757 S 00:00:00 bash
4987 T 00:00:00 man ps
4997 T 00:00:00 pager
5508 R 00:00:00 ps -o pid,state,time,command
ps -o pid,ppid,wchan,comm
PID PPID WCHAN COMMAND
1757 1749 wait bash
4987 1757 signal man
4997 4987 signal pager
5510 1757 - ps
ps
PID TTY TIME CMD
1757 pts/0 00:00:00 bash
4987 pts/0 00:00:00 man
4997 pts/0 00:00:00 pager
5307 pts/0 00:00:00 ps
ps -c
PID CLS PRI TTY TIME CMD
1757 TS 19 pts/0 00:00:00 bash
4987 TS 19 pts/0 00:00:00 man
4997 TS 19 pts/0 00:00:00 pager
5282 TS 19 pts/0 00:00:00 ps
CLS:进程的调度类,可取:
- not reported 未报告
TS SCHED_OTHER 其他计划
FF SCHED_FIFO FF计划
RR SCHED_RR RR计划
B SCHED_BATCH 进度计划
ISO SCHED_ISO ISO计划
IDL SCHED_IDLE IDL调度空闲
? unknown value 未知值
ps -e 显示全部
PID TTY TIME CMD
1 ? 00:00:07 systemd
2 ? 00:00:00 kthreadd
3 ? 00:00:00 rcu_gp
4 ? 00:00:00 rcu_par_gp
6 ? 00:00:00 kworker/0:0H-kb
7 ? 00:01:06 kworker/0:1-eve
9 ? 00:00:00 mm_percpu_wq
10 ? 00:00:01 ksoftirqd/0
11 ? 00:00:04 rcu_sched
12 ? 00:00:01 migration/0
13 ? 00:00:00 idle_inject/0
14 ? 00:00:00 cpuhp/0
ps -f
UID PID PPID C STIME TTY TIME CMD
zyy 1757 1749 0 08:45 pts/0 00:00:00 bash
zyy 4987 1757 0 18:13 pts/0 00:00:00 man ps
zyy 4997 4987 0 18:13 pts/0 00:00:00 pager
zyy 5305 1757 0 20:38 pts/0 00:00:00 ps -f
ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 08:43 ? 00:00:07 /sbin/init splash
root 2 0 0 08:43 ? 00:00:00 [ kthreadd]
root 3 2 0 08:43 ? 00:00:00 [ rcu_gp]
root 4 2 0 08:43 ? 00:00:00 [ rcu_par_gp]
root 6 2 0 08:43 ? 00:00:00 [ kworker/0:0H-kb]
root 7 2 0 08:43 ? 00:01:06 [ kworker/0:1-eve]
root 9 2 0 08:43 ? 00:00:00 [ mm_percpu_wq]
root 10 2 0 08:43 ? 00:00:01 [ ksoftirqd/0]
root 11 2 0 08:43 ? 00:00:04 [ rcu_sched]
root 12 2 0 08:43 ? 00:00:01 [ migration/0]
root 13 2 0 08:43 ? 00:00:00 [ idle_inject/0]
root 14 2 0 08:43 ? 00:00:00 [ cpuhp/0]
root 15 2 0 08:43 ? 00:00:00 [ kdevtmpfs]
ps -l
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
0 S 1000 1757 1749 0 80 0 - 7493 wait pts/0 00:00:00 bash
0 T 1000 4987 1757 0 80 0 - 7109 signal pts/0 00:00:00 man
0 T 1000 4997 4987 0 80 0 - 4238 signal pts/0 00:00:00 pager
0 R 1000 5284 1757 0 80 0 - 9006 - pts/0 00:00:00 ps
说明:各相关信息的意义:
F 代表这个程序的旗标 ( flag) , 4 代表使用者为 super user
S 代表这个程序的状态 ( STAT)
UID 程序被该 UID 所拥有
PID 就是这个程序的 ID !
PPID 则是其上级父程序的ID
C CPU 使用的资源百分比
PRI 这个是 Priority ( 优先执行序) 的缩写
NI 这个是 Nice 值,
ADDR 这个是 kernel function,指出该程序在内存的那个部分。如果是个 running的程序,一般就是 "-"
SZ 使用掉的内存大小
WCHAN 进程正在睡眠的内核函数的名称,若为 - 表示正在运作,“*”如果进程是多线程的,并且ps不显示线程。
TTY 登入者的终端机位置
TIME 使用掉的 CPU 时间。
CMD 所下达的指令为何
ps c
PID TTY STAT TIME COMMAND
1257 tty2 Ssl+ 0:00 gdm-x-session
1259 tty2 Sl+ 3:11 Xorg
1266 tty2 Sl+ 0:00 gnome-session-b
1409 tty2 Sl+ 8:08 gnome-shell
1440 tty2 Sl 1:04 ibus-daemon
1444 tty2 Sl 0:00 ibus-dconf
1446 tty2 Sl 0:00 ibus-x11
1519 tty2 Sl+ 0:01 gsd-power
1521 tty2 Sl+ 0:00 gsd-print-notif
ps a
PID TTY STAT TIME COMMAND
1041 tty1 Ssl+ 0:00 /usr/lib/gdm3/gdm-wayland-session gnome-session --au
1045 tty1 Sl+ 0:00 /usr/lib/gnome-session/gnome-session-binary --autost
1051 tty1 Sl+ 0:31 /usr/bin/gnome-shell
1068 tty1 S+ 0:00 /usr/bin/Xwayland :1024 -rootless -terminate -access
1096 tty1 Sl 0:00 ibus-daemon --xim --panel disable
1099 tty1 Sl 0:00 /usr/lib/ibus/ibus-dconf
1101 tty1 Sl 0:00 /usr/lib/ibus/ibus-x11 --kill-daemon
1131 tty1 Sl+ 0:00 /usr/lib/gnome-settings-daemon/gsd-xsettings
1133 tty1 Sl+ 0:00 /usr/lib/gnome-settings-daemon/gsd-a11y-settings
1134 tty1 Sl+ 0:00 /usr/lib/gnome-settings-daemon/gsd-clipboard
1136 tty1 Sl+ 0:14 /usr/lib/gnome-settings-daemon/gsd-color
1138 tty1 Sl+ 0:00 /usr/lib/gnome-settings-daemon/gsd-datetime
ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.3 225364 7096 ? Ss 08:43 0:07 /sbin/init spl
root 2 0.0 0.0 0 0 ? S 08:43 0:00 [ kthreadd]
root 3 0.0 0.0 0 0 ? I< 08:43 0:00 [ rcu_gp]
root 4 0.0 0.0 0 0 ? I< 08:43 0:00 [ rcu_par_gp]
root 6 0.0 0.0 0 0 ? I< 08:43 0:00 [ kworker/0:0H-
root 7 0.1 0.0 0 0 ? I 08:43 1:06 [ kworker/0:1-e
root 9 0.0 0.0 0 0 ? I< 08:43 0:00 [ mm_percpu_wq]
root 10 0.0 0.0 0 0 ? S 08:43 0:01 [ ksoftirqd/0]
root 11 0.0 0.0 0 0 ? I 08:43 0:04 [ rcu_sched]
root 12 0.0 0.0 0 0 ? S 08:43 0:01 [ migration/0]
root 13 0.0 0.0 0 0 ? S 08:43 0:00 [ idle_inject/0
root 14 0.0 0.0 0 0 ? S 08:43 0:00 [ cpuhp/0]
root 15 0.0 0.0 0 0 ? S 08:43 0:00 [ kdevtmpfs]
root 16 0.0 0.0 0 0 ? I< 08:43 0:00 [ netns]
ps l
F UID PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME COMMAND
4 1000 1257 1236 20 0 212248 6160 poll_s Ssl+ tty2 0:00 /usr/
4 1000 1259 1257 20 0 442784 68292 ep_pol Sl+ tty2 2:44 /usr/
0 1000 1266 1257 20 0 560000 11632 poll_s Sl+ tty2 0:00 /usr/
0 1000 1409 1266 20 0 3070072 254096 poll_s Sl+ tty2 7:31 /usr/
0 1000 1440 1409 20 0 365392 15428 poll_s Sl tty2 1:03 ibus-
0 1000 1444 1440 20 0 280988 6420 poll_s Sl tty2 0:00 /usr/
0 1000 1446 1 20 0 344672 15652 poll_s Sl tty2 0:00 /usr/
0 1000 1519 1266 20 0 798940 25468 poll_s Sl+ tty2 0:01 /usr/
0 1000 1521 1266 20 0 349472 8280 poll_s Sl+ tty2 0:00 /usr/
0 1000 1522 1266 20 0 423492 5916 poll_s Sl+ tty2 0:00 /usr/
0 1000 1523 1266 20 0 275880 5980 poll_s Sl+ tty2 0:00 /usr/
0 1000 1524 1266 20 0 453008 7372 poll_s Sl+ tty2 0:00 /usr/
符号说明:
标志 含义 D uninterruptible sleep (usually IO)不间断睡眠(通常为IO) R running or runnable (on run queue) S interruptible sleep (waiting for an event to complete)可中断睡眠(等待事件完成) T stopped by job control signal t stopped by debugger during the tracing W paging (not valid since the 2.6.xx kernel) X dead (should never be seen) Z defunct (“zombie”) process, terminated but not reaped by its parent已失效(“僵尸”)进程,已终止但未被其父进程获取
标志 含义 < high-priority (not nice to other users) N low-priority (nice to other users) L has pages locked into memory (for real-time and custom IO)将页面锁定到内存中(用于实时和自定义IO) s is a session leader是会话期首进程 l is multi-threaded (using CLONE_THREAD, like NPTL pthreads do)是多线程 + is in the foreground process group在前台进程组
其余见手册 man ps