1.“ps --help”


1.1.简单选择

1.1.1.显示所有进程

[root@tri work]# ps -e
[root@tri work]# ps -e | wc -l
73
[work@tri ~]$ ps -e | wc -l
73


1.1.2.终端下、排除会话头

[root@tri work]# ps -a
  PID TTY          TIME CMD
 5729 pts/0    00:00:00 su
 5732 pts/0    00:00:00 bash
 5823 pts/0    00:00:00 ps


1.1.3.终端下,包含其他用户

[root@tri work]# ps a
  PID TTY      STAT   TIME COMMAND
  461 tty1     Ss+    0:00 /sbin/agetty --noclear tty1 linux
 5566 pts/0    Ss     0:00 -bash
 5622 pts/1    Ss+    0:00 -bash
 5729 pts/0    S      0:00 su
 5732 pts/0    S      0:00 bash
 5824 pts/0    R+     0:00 ps a


1.1.4.所有进程,排除会话头

[root@tri work]# ps -d


1.1.5.正在运行的进程

[work@oxo bean]$ ps r
  PID TTY      STAT   TIME COMMAND
30090 pts/1    D+     0:35 md5sum tmp/app47.tar
30093 pts/0    R+     0:00 ps r



1.2.按列表选择

1.2.1.按照命令名称

[root@tri work]# ps -C httpd
[root@tri work]# ps -C vsftpd


1.2.2.按照“组ID”、“组名”

[root@tri work]# ps -G apache
[root@tri work]# ps -G 48


1.2.3.按照进程号显示

显示进程5202的信息
[root@tri work]# ps -p 5202
显示5202的子进程信息
[root@tri work]# ps --ppid 5202


1.2.4.显示某个终端的进程

[work@tri ~]$ ps -t pts/0
  PID TTY          TIME CMD
 4967 pts/0    00:00:00 bash
 5013 pts/0    00:00:00 su
 5014 pts/0    00:00:00 bash
 5304 pts/0    00:00:11 java
显示没有终端信息的进程
 [work@tri ~]$ ps -t ?


1.2.5.按照“用户id”、“用户名”

[work@tri ~]$ ps -u work
  PID TTY          TIME CMD
 5506 ?        00:00:11 java
 5565 ?        00:00:00 sshd
 5566 pts/0    00:00:00 bash
 5598 pts/0    00:00:00 ps
[work@tri ~]$ ps -U work
  PID TTY          TIME CMD
 5506 ?        00:00:11 java
 5565 ?        00:00:00 sshd
 5566 pts/0    00:00:00 bash
 5599 pts/0    00:00:00 ps
 [work@tri ~]$ ps -U 1000
  PID TTY          TIME CMD
 5506 ?        00:00:12 java
 5565 ?        00:00:00 sshd
 5566 pts/0    00:00:00 bash
 5600 pts/0    00:00:00 ps



1.3.输出

1.3.1.固定格式输出

[root@tri work]# ps -C httpd -F
UID        PID  PPID  C    SZ   RSS PSR STIME TTY          TIME CMD
root      5913     1  0 56553  5164   0 20:46 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache    5914  5913  0 56553  3008   0 20:46 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache    5915  5913  0 56553  3008   0 20:46 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache    5916  5913  0 56553  3008   0 20:46 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache    5917  5913  0 56553  3008   0 20:46 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache    5918  5913  0 56553  3008   0 20:46 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND



[root@tri work]# ps -C httpd -f
UID        PID  PPID  C STIME TTY          TIME CMD
root      5913     1  0 20:46 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache    5914  5913  0 20:46 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache    5915  5913  0 20:46 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache    5916  5913  0 20:46 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache    5917  5913  0 20:46 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache    5918  5913  0 20:46 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND



[root@tri work]# ps -C httpd -l
F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
4 S     0  5913     1  0  80   0 - 56553 poll_s ?        00:00:00 httpd
5 S    48  5914  5913  0  80   0 - 56553 inet_c ?        00:00:00 httpd
5 S    48  5915  5913  0  80   0 - 56553 inet_c ?        00:00:00 httpd
5 S    48  5916  5913  0  80   0 - 56553 inet_c ?        00:00:00 httpd
5 S    48  5917  5913  0  80   0 - 56553 inet_c ?        00:00:00 httpd
5 S    48  5918  5913  0  80   0 - 56553 inet_c ?        00:00:00 httpd



[root@tri work]# ps -C httpd s
  UID   PID          PENDING          BLOCKED          IGNORED           CAUGHT STAT TTY        TIME COMMAND
    0  5913 0000000000000000 0000000000000000 0000000001001000 00000001880046eb Ss   ?          0:00 /usr/sbin/httpd -DFOREGROUND
   48  5914 0000000000000000 0000000000000000 0000000001001002 00000001880046e9 S    ?          0:00 /usr/sbin/httpd -DFOREGROUND
   48  5915 0000000000000000 0000000000000000 0000000001001002 00000001880046e9 S    ?          0:00 /usr/sbin/httpd -DFOREGROUND
   48  5916 0000000000000000 0000000000000000 0000000001001002 00000001880046e9 S    ?          0:00 /usr/sbin/httpd -DFOREGROUND
   48  5917 0000000000000000 0000000000000000 0000000001001002 00000001880046e9 S    ?          0:00 /usr/sbin/httpd -DFOREGROUND
   48  5918 0000000000000000 0000000000000000 0000000001001002 00000001880046e9 S    ?          0:00 /usr/sbin/httpd -DFOREGROUND



[root@tri work]# ps -C httpd u
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root      5913  0.0  0.5 226212  5164 ?        Ss   20:46   0:00 /usr/sbin/httpd -DFOREGROUND
apache    5914  0.0  0.2 226212  3008 ?        S    20:46   0:00 /usr/sbin/httpd -DFOREGROUND
apache    5915  0.0  0.2 226212  3008 ?        S    20:46   0:00 /usr/sbin/httpd -DFOREGROUND
apache    5916  0.0  0.2 226212  3008 ?        S    20:46   0:00 /usr/sbin/httpd -DFOREGROUND
apache    5917  0.0  0.2 226212  3008 ?        S    20:46   0:00 /usr/sbin/httpd -DFOREGROUND
apache    5918  0.0  0.3 226348  3252 ?        S    20:46   0:00 /usr/sbin/httpd -DFOREGROUND



[root@tri work]# ps -C httpd v
  PID TTY      STAT   TIME  MAJFL   TRS   DRS   RSS %MEM COMMAND
 5913 ?        Ss     0:00      0   480 225731 5164  0.5 /usr/sbin/httpd -DFOREGROUND
 5914 ?        S      0:00      0   480 225731 3008  0.2 /usr/sbin/httpd -DFOREGROUND
 5915 ?        S      0:00      0   480 225731 3008  0.2 /usr/sbin/httpd -DFOREGROUND
 5916 ?        S      0:00      0   480 225731 3008  0.2 /usr/sbin/httpd -DFOREGROUND
 5917 ?        S      0:00      0   480 225731 3008  0.2 /usr/sbin/httpd -DFOREGROUND
 5918 ?        S      0:00      0   480 225867 3252  0.3 /usr/sbin/httpd -DFOREGROUND



[root@tri work]# ps -C httpd X
  PID   STACKP      ESP      EIP TMOUT ALARM STAT TTY        TIME COMMAND
 5913 4d562100 4d561d58 81b45b83     -     - Ss   ?          0:00 /usr/sbin/httpd -DFOREGROUND
 5914 4d562100 4d561b48 81b4fb77     -     - S    ?          0:00 /usr/sbin/httpd -DFOREGROUND
 5915 4d562100 4d561b48 81b4fb77     -     - S    ?          0:00 /usr/sbin/httpd -DFOREGROUND
 5916 4d562100 4d561b48 81b4fb77     -     - S    ?          0:00 /usr/sbin/httpd -DFOREGROUND
 5917 4d562100 4d561b48 81b4fb77     -     - S    ?          0:00 /usr/sbin/httpd -DFOREGROUND
 5918 4d562100 4d561b48 81b4fb77     -     - S    ?          0:00 /usr/sbin/httpd -DFOREGROUND



1.3.2.自定义格式输出

  使用“o”定义输出格式

[root@tri work]# ps -C httpd -o user,group,stime
USER     GROUP    STIME
root     root     20:46
apache   apache   20:46
apache   apache   20:46
apache   apache   20:46
apache   apache   20:46
apache   apache   20:46



2.常用操作



 

简单进程选择

  -e 显示所有进程

  ax 显示所有进程(BSD模式)

 

列表进程选择

  -C 显示指定命令的进程

    eg: ps -C bash -o pid,comm,%cpu,%mem

    eg: ps -C java -o pid,comm,%cpu,%mem

  -G 显示用户组的进程

    eg: ps -G bea -o pid,%cpu,%mem,time,cmd,comm

  -U  显示用户的进程

    eg: ps -U weblogic -o pid,%cpu,%mem,stat

  -p “pid”列表指定的进程

  -s 会话列表指定的进程

  --ppid 选择子进程信息

    eg: ps --ppid 1 -o pid,ppid,comm,cmd

  -123 会话id

  123 进程id

  -t 查看某个终端的进程


找出运行java的进程号

[root@iZ28lyaw0o0Z ~]# ps -C java -o pid=
  913
 6988


输出格式控制

  -f

  -j

  e 在命令后显示其环境变量

  ww 不限制宽度输出

 

进程状态代码

  D 不可中断睡眠

  R 运行

  S 等待终止信号的睡眠

  Z 僵尸进程

  T 停止的

    BSD信息

     < 高特权

     N 低特权

     L

     s 会话头

     l 多线程

     + 前台进程组中的进程

     [ ] 内核线程

 

[root@iZ28fpqf1fgZ ~]# tar -cf 131-home-d.tar /home/ &
[root@iZ28fpqf1fgZ ~]# ps -p 4668 -o pid,stat,%mem,%cpu,cmd
  PID STAT %MEM %CPU CMD
 4668 D     0.0  5.6 tar -cf 131-home-d.tar /home/
[root@iZ28fpqf1fgZ ~]# kill -9 4668
[root@iZ28fpqf1fgZ ~]# ps -p 4668 -o pid,stat,%mem,%cpu,cmd
  PID STAT %MEM %CPU CMD
[1]+  Killed                  tar -cf 131-home-d.tar /home/
[root@iZ28fpqf1fgZ ~]# ps -p 4668 -o pid,stat,%mem,%cpu,cmd
  PID STAT %MEM %CPU CMD



其他信息

  L 显示可以指定的格式列名

    eg: ps L

    eg: ps -e -o uid,user,pid,cpuid,cputime,start_time,start,cmd

  找出僵死进程

# ps -e -o pid,stat | grep -i n
   282 SN
   283 SN

  BSD模式

# ps ax o pid,stat


 

显示结果

  %CPU 使用掉处理器时间的百分比

  %MEM 使用了多少物理内存的百分比

  VSZ 进程请求虚拟内存使用的数量(KB

  RSS KB

  SIZE 内存占用大小KB

  TIME 使用的cpu时间总量(自启动)

  STIME 进程启动时间(年)

  START 进程的启动时间(月日)

  STAT 进程状态

    eg: ps -C bash -o pid,rss,vsz,size,cmd