进程查看管理工具之ps/top/dstat/htop命令详解


一、ps命令详解:

ps 加选项参数

ps a 显示与终端相关的内容信息,例如:

[root@server cx]# ps a

  PID TTY      STAT   TIME COMMAND

  2396 tty3     Ss+    0:00 /sbin/mingetty /dev/tty3

  2399 tty4     Ss+    0:00 /sbin/mingetty /dev/tty4

  2401 tty5     Ss+    0:00 /sbin/mingetty /dev/tty5

  2404 tty6     Ss+    0:00 /sbin/mingetty /dev/tty6

  2472 tty1     Ss+    0:02 /usr/bin/Xorg :0 -br -verbose -audit 4 -auth /var/

  5095 pts/0    Ss     0:02 -bash

  6285 tty2     Ss+    0:00 -bash

  8612 pts/0    R+     0:00 ps a

ps  x  显示与终端无关的内容信息,例如:


[root@server cx]# ps x

    PID TTY      STAT   TIME COMMAND

     1 ?        Ss     0:02 /sbin/init

     2 ?        S      0:00 [kthreadd]

     3 ?        S      0:00 [migration/0]

     4 ?        S      0:00 [ksoftirqd/0]

     5 ?        S      0:00 [stopper/0]

     6 ?        S      0:00 [watchdog/0]

     7 ?        S      0:00 [migration/1]

     8 ?        S      0:00 [stopper/1]

     9 ?        S      0:00 [ksoftirqd/1]

    10 ?        S      0:00 [watchdog/1]

ps u 以用户为中心组织进程状态信息显示;

[root@server cx]# ps u

USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND

root       2396  0.0  0.0   4064   552 tty3     Ss+  08:05   0:00 /sbin/mingetty /dev/tty3

root       2399  0.0  0.0   4064   552 tty4     Ss+  08:05   0:00 /sbin/mingetty /dev/tty4

root       2401  0.0  0.0   4064   552 tty5     Ss+  08:05   0:00 /sbin/mingetty /dev/tty5

root       2404  0.0  0.0   4064   552 tty6     Ss+  08:05   0:00 /sbin/mingetty /dev/tty6


[root@server cx]# ps aux 查看列出所有信息。

USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND

root          1  0.0  0.1  19360  1616 ?        Ss   08:04   0:02 /sbin/init

root          2  0.0  0.0      0     0 ?        S    08:04   0:00 [kthreadd]

root          3  0.0  0.0      0     0 ?        S    08:04   0:00 [migration/]

root          4  0.0  0.0      0     0 ?        S    08:04   0:00 [ksoftirqd/]

root          5  0.0  0.0      0     0 ?        S    08:04   0:00 [stopper/0]

root          6  0.0  0.0      0     0 ?        S    08:04   0:00 [watchdog/0]

root          7  0.0  0.0      0     0 ?        S    08:04   0:00 [migration/]

root          8  0.0  0.0      0     0 ?        S    08:04   0:00 [stopper/1]

USER  以用户为中心

PID为进程号码  

%CPU 所用的比率

 %MEM 累计内存占用比率

以上所显示的的信息依次为:

VSZ:虚拟内存集;

RSS:Resident Size,常驻内存集; 

STAT:状态分一下集中情况:

R:running  运行态

S:interruptable sleeping  可中断睡眠

D:uninterruptable sleeping  不可中断睡眠

T:Stopped 停止态

Z:zombie 僵死态

+:前台进程

l:多线程进程

N:低优先级进程f

<:高优先级进程  

s:session leader 回话主导者

ps -e  命令的意思是显示所有进程:

[root@server cx]# ps -e

   PID TTY          TIME CMD

     1 ?        00:00:02 init

     2 ?        00:00:00 kthreadd

     3 ?        00:00:00 migration/0

     4 ?        00:00:00 ksoftirqd/0

     5 ?        00:00:00 stopper/0

     6 ?        00:00:00 watchdog/0

     7 ?        00:00:00 migration/1

     8 ?        00:00:00 stopper/1

     9 ?        00:00:00 ksoftirqd/1

    10 ?        00:00:00 watchdog/1

    11 ?        00:00:48 events/0

    12 ?        00:00:06 events/1

    13 ?        00:00:00 events/0

    14 ?        00:00:00 events/1

    15 ?        00:00:00 events_long/

ps -ef 显示更为详细的所有进程信息:

[root@server cx]# ps -ef 

UID         PID   PPID  C STIME TTY          TIME CMD

root          1      0  0 08:04 ?        00:00:02 /sbin/init

root          2      0  0 08:04 ?        00:00:00 [kthreadd]

root          3      2  0 08:04 ?        00:00:00 [migration/0]

root          4      2  0 08:04 ?        00:00:00 [ksoftirqd/0]

root          5      2  0 08:04 ?        00:00:00 [stopper/0]

root          6      2  0 08:04 ?        00:00:00 [watchdog/0]

root          7      2  0 08:04 ?        00:00:00 [migration/1]

root          8      2  0 08:04 ?        00:00:00 [stopper/1]

root          9      2  0 08:04 ?        00:00:00 [ksoftirqd/1]

root         10      2  0 08:04 ?        00:00:00 [watchdog/1]

root         11      2  0 08:04 ?        00:00:48 [events/0]

root         12      2  0 08:04 ?        00:00:06 [events/1]

root         13      2  0 08:04 ?        00:00:00 [events/0]

root         14      2  0 08:04 ?        00:00:00 [events/1]

root         15      2  0 08:04 ?        00:00:00 [events_long/0]

root         16      2  0 08:04 ?        00:00:00 [events_long/1]

root         17      2  0 08:04 ?        00:00:00 [events_power_ef]

root         18      2  0 08:04 ?        00:00:00 [events_power_ef]

root         19      2  0 08:04 ?        00:00:00 [cgroup]

root         20      2  0 08:04 ?        00:00:00 [khelper]

ps -eF 命令的意思是显示更详细进程的信息:

[root@server cx]# ps -eF

UID         PID   PPID  C    SZ   RSS PSR STIME TTY          TIME CMD

root          1      0  0  4840  1616   1 08:04 ?        00:00:02 /sbin/init

root          2      0  0     0     0   1 08:04 ?        00:00:00 [kthreadd]

root          3      2  0     0     0   0 08:04 ?        00:00:00 [migration/]

root          4      2  0     0     0   0 08:04 ?        00:00:00 [ksoftirqd/]

root          5      2  0     0     0   0 08:04 ?        00:00:00 [stopper/0]

root          6      2  0     0     0   0 08:04 ?        00:00:00 [watchdog/0]

root          7      2  0     0     0   1 08:04 ?        00:00:00 [migration/]

root          8      2  0     0     0   1 08:04 ?        00:00:00 [stopper/1]

root          9      2  0     0     0   1 08:04 ?        00:00:00 [ksoftirqd/]

root         10      2  0     0     0   1 08:04 ?        00:00:00 [watchdog/1]

root         11      2  0     0     0   0 08:04 ?        00:00:48 [events/0]

root         12      2  0     0     0   1 08:04 ?        00:00:06 [events/1]

root         13      2  0     0     0   0 08:04 ?        00:00:00 [events/0]

root         14      2  0     0     0   1 08:04 ?        00:00:00 [events/1]

root         15      2  0     0     0   0 08:04 ?        00:00:00 [events_lon]

root         16      2  0     0     0   1 08:04 ?        00:00:00 [events_lon]

root         17      2  0     0     0   0 08:04 ?        00:00:00 [events_pow]

root         18      2  0     0     0   1 08:04 ?        00:00:00 [events_pow]

ps -eFH命令的意思是以层级结构显示进程的相关信息。

ps -o +字段+字段,,;自定义要显示的字段列表,以逗号分隔。


常用的字段有:field:pid, ni, pri, psr, pcpu, stat, comm, tty, ppid, rtprioni:nice值;

priority:priority, 优先级;rtprio:real time priority,实时优先级

ps -eo

ps -axo + pid,canmand 显示进程号和command 两个信息

[root@server cx]# ps -axo pid,command

Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ

   PID COMMAND

     1 /sbin/init

     2 [kthreadd]

     3 [migration/0]

     4 [ksoftirqd/0]

     5 [stopper/0]

     6 [watchdog/0]

     7 [migration/1]

     8 [stopper/1]

     9 [ksoftirqd/1]

    10 [watchdog/1]

    11 [events/0]

二、top命令常用参数选项

top + -d #:指定刷新时间间隔,默认为3秒;如下是命令使用格式:

 [root@server cx]# top -d 5

top + -b:以批次方式显示;如下是命令使用格式:

[root@server cx]# top -b

 top + -n #:显示多少批次;如下是命令使用格式:

[root@server cx]# top -n 2

三、dstat命令常用参数选项

dstat命令:

- versatile tool for generating system resource statistics

dstat [-afv] [options..] [delay [count]]

常用选项:

dstat -c, --cpu:显示cpu相关信息;例如:

[root@server cx]# dstat -c

----total-cpu-usage----

usr sys idl wai hiq siq

  0   0 100   0   0   0

  0   0 100   0   0   0

  0   0 100   0   0   0

  0   0 100   0   0   0

  0   0 100   0   0   0

  0   1 100   0   0   0

  0   0 100   0   0   0

dstat -C #,#,...,total 显示第几号CPU的相关信息:例如:

[root@server cx]# dstat -C 1

-------cpu1-usage------ -dsk/total- -net/total- ---paging-- ---system--

usr sys idl wai hiq siq| read  writ| recv  send|  in   out | int   csw 

  0   0 100   0   0   0|8241B 2672B|   0     0 |   0     0 |  28    29 

  0   0 100   0   0   0|   0     0 | 180B  826B|   0     0 |  22    21 

  0   0 100   0   0   0|   0     0 | 180B  346B|   0     0 |  40    37 

  0   0 100   0   0   0|   0     0 |  60B  346B|   0     0 |  95    69 

  0   0 100   0   0   0|   0     0 | 180B  346B|   0     0 |  35    32 

  0   0 100   0   0   0|   0     0 |  60B  346B|   0     0 |  21    15 

  0   0 100   0   0   0|   0     0 |  60B  346B|   0     0 |  33    36 

  0   0 100   0   0   0|   0     0 |  60B  346B|   0     0 |  16    13 ^C

dstat -d, --disk:显示磁盘的相关信息

[root@server cx]# dstat -d

-dsk/total-

 read  writ

8227B 2672B

   0     0 

   0     0 ^C

[root@server cx]# dstat --disk

-dsk/total-

 read  writ

8226B 2671B

   0     0 

   0     0 

   0     0 ^C

dstat -D sda,sdb,...,tobal   (命令的使用方法一样)

dstat -g:显示page相关的速率数据;(命令的使用方法一样)

dstat -m:Memory的相关统计数据 :(命令的使用方法一样)

dstat -n:Interface的相关统计数据; (命令的使用方法一样)

dstat -p:显示process的相关统计数据; (命令的使用方法一样)

dstat -r:显示io请求的相关的统计数据;(命令的使用方法一样)

dstat -s:显示swapped的相关统计数据;(命令的使用方法一样)

[root@server cx]# dstat -s

----swap---

 used  free

   0  2000M

   0  2000M

   0  2000M

   0  2000M

   0  2000M

--tcp 

[root@server cx]# dstat --tcp

----tcp-sockets----

lis act syn tim clo

 11   1   0   0   0

 11   1   0   0   0

 11   1   0   0   0

 11   1   0   0   0

 11   1   0   0   0

 11   1   0   0   0

 11   1   0   0   0

 11   1   0   0   0


dstat --udp 显示upd相关的信息

dstat --raw  显示raw相关的信息

dstat --socket 显示socket相关的信息

dstat --ipc  显示ipc相关的信息

[root@server cx]# dstat --ipc

--sysv-ipc-

msg sem shm

  0   2   4

  0   2   4

  0   2   4

  0   2   4

  0   2   4

  0   2   4

  0   2   4

dstat --top-cpu:显示最占用CPU的进程,例如:

[root@server cx]# dstat --top-cpu

-most-expensive-

  cpu process   

events/0     0.1



dstat --top-io:最占用io的进程;

[root@server cx]# dstat --top-io

----most-expensive----

     i/o process      

init       1835B 2785B

irqbalance 7620B    0 

ksmtuned    126k   10B

sshd: root@  78B  116B

sshd: root@  78B  116B

sshd: root@  78B  116B



dstat --top-mem:最占用内存的进程;

[root@server cx]# dstat --top-mem

--most-expensive-

  memory process 

Xorg        22.0M

Xorg        22.0M

Xorg        22.0M

Xorg        22.0M


dstat --top-lantency:延迟最大的进程;


四、htop命令使用详解

htop命令常用选项参数如下:

选项:

-d #:指定延迟时间间隔;命令的语法如下所示:

htop -d  3  间隔时间为3S

-u UserName:仅显示指定用户的进程;命令的语法如下所示:

htop -u wen 显示用户名为wen的进程信息

-s COLUME:以指定字段进行排序;

子命令:

l:显示选定的进程打开的文件列表;

s:跟踪选定的进程的系统调用;

t:以层级关系显示各进程状态;

a:将选定的进程绑定至某指定的CPU核心;


好了,今天就写到这里吧,这是经常用的一些linux常用管理工具,跟着马哥继续努力吧。