25.7. 进程列表:ps

命令 ps 生成进程的列表。如果添加了参数 r,则只显示当前正在使用计算时间的进程:

$ ps r 
PID TTY      STAT   TIME COMMAND 
22163 pts/7    R      0:01 -zsh 
3396 pts/3    R      0:03 emacs new-makedoc.txt 
20027 pts/7    R      0:25 emacs xml/common/utilities.xml 
20974 pts/7    R      0:01 emacs jj.xml 
27454 pts/7    R      0:00 ps r 

书写此参数时一定不能带减号。不同的参数在书写时有时带减号,有时不带减号。手册页中有关此问题的复杂说明很容易让潜在用户望而止步,但幸好 ps --help 命令可以生成简要的帮助页。

要检查有多少 emacs 进程正在运行,请使用:

$ ps x | grep emacs 
1288 ?S      0:07 emacs 
3396 pts/3    S      0:04 emacs new-makedoc.txt 
3475 ?S      0:03 emacs .Xresources 
20027 pts/7    S      0:40 emacs xml/common/utilities.xml 
20974 pts/7    S      0:02 emacs jj.xml

$ pidof emacs 
20974 20027 3475 3396 1288 

参数 -p 通过进程 ID 选择进程:

$ ps www -p $(pidof xterm) 
PID TTY      STAT   TIME COMMAND 
 9025 ?S      0:01 xterm -g 100x45+0+200 
 9176 ?S      00:00:00 xterm -g 100x45+0+200 
29854 ?S      0:21 xterm -g 100x75+20+0 -fn \ 
  -B&H-LucidaTypewriter-Medium-R-Normal-Sans-12-120-75-75-M-70-iso10646-1 
 4378 ?S      0:01 xterm -bg MistyRose1 -T root -n root -e su -l 
25543 ?S      00:02:00 xterm -g 100x45+0+200 
22161 ?R      0:14 xterm -g 100x45+0+200 
16832 ?S      0:01 xterm -bg MistyRose1 -T root -n root -e su -l 
16912 ?S      00:00:00 xterm -g 100x45+0+200 
17861 ?S      0:00 xterm -bg DarkSeaGreen1 -g 120x45+40+300 
19930 ?S      0:13 xterm -bg LightCyan 
21686 ?S      0:04 xterm -g 100x45+0+200 -fn \ 
  lucidasanstypewriter-12 
23104 ?S      00:00:00 xterm -g 100x45+0+200 
26547 ?S      0:00 xterm -g 100x45+0+200 

可以根据需要设置进程列表的格式。选项 -L 返回所有关键字的列表。输入以下命令可以生成所有进程按内存使用量排序的列表:

$ ps ax --format pid,rss,cmd --sort rss 
  PID  RSS CMD 
    2    0 [ksoftirqd/0] 
    3    0 [events/0] 
    17    0 [kblockd/0] 
[...] 
10164 5260 xterm 
31110 5300 xterm 
17010 5356 xterm 
3896 29292 /usr/X11R6/bin/X -nolisten tcp -br vt7 -auth /var/lib/xdm/authdir/au