Linux运维平台工具:pstree、ps、top、htop、free、vmstat、dstat、kill、killall

1、pstree命令

查看进程树,centos7下统一由systemd进行管理

[root@ELK-chaofeng systemd]# pstree
systemd─┬─AliYunDun───18*[{AliYunDun}]
        ├─AliYunDunUpdate───3*[{AliYunDunUpdate}]
        ├─CmsGoAgent.linu───9*[{CmsGoAgent.linu}]
        ├─2*[agetty]
        ├─aliyun-service───2*[{aliyun-service}]
        ├─atd
        ├─crond
        ├─dbus-daemon
        ├─dhclient
        ├─httpd───httpd───101*[{httpd}]
        ├─mysqld_safe───mysqld───28*[{mysqld}]
        ├─ntpd
        ├─php-fpm───2*[php-fpm]
        ├─polkitd───5*[{polkitd}]
        ├─rsyslogd───2*[{rsyslogd}]
        ├─sshd─┬─sshd───bash
        │      └─sshd───bash───pstree
        ├─staragentd─┬─staragent-core───40*[{staragent-core}]
        │            └─staragent-ppf───11*[{staragent-ppf}]
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-udevd
        └─tuned───4*[{tuned}]

2、ps命令详解

ps命令有BSD风格和UNIX风格,BSD风格中,选项参数后面是不加“-”的,而UNIX风格中,选项要加上“-”,使用不同的风格,选项参数的意义是不一样的,并且大多数情况下我们使用BSD风格比较多,我们来看一下。

1)显示进程的PID,进程对应的命令,CPU利用率等信息

[root@ELK-chaofeng tmp]# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.1  43144  3604 ?        Ss   Feb02   0:07 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
root         2  0.0  0.0      0     0 ?        S    Feb02   0:00 [kthreadd]
root         3  0.0  0.0      0     0 ?        S    Feb02   0:13 [ksoftirqd/0]
root         5  0.0  0.0      0     0 ?        S<   Feb02   0:00 [kworker/0:0H]
root         7  0.0  0.0      0     0 ?        S    Feb02   0:00 [migration/0]
root         8  0.0  0.0      0     0 ?        S    Feb02   0:00 [rcu_bh]
root         9  0.0  0.0      0     0 ?        R    Feb02   0:55 [rcu_sched]
root        10  0.0  0.0      0     0 ?        S    Feb02   0:02 [watchdog/0]
root        12  0.0  0.0      0     0 ?        S    Feb02   0:00 [kdevtmpfs]
root        13  0.0  0.0      0     0 ?        S<   Feb02   0:00 [netns]
root        14  0.0  0.0      0     0 ?        S    Feb02   0:00 [khungtaskd]
root        15  0.0  0.0      0     0 ?        S<   Feb02   0:00 [writeback]
root        16  0.0  0.0      0     0 ?        S<   Feb02   0:00 [kintegrityd]
root        17  0.0  0.0      0     0 ?        S<   Feb02   0:00 [bioset]
root        18  0.0  0.0      0     0 ?        S<   Feb02   0:00 [kblockd]
root        19  0.0  0.0      0     0 ?        S<   Feb02   0:00 [md]
root        25  0.0  0.0      0     0 ?        S    Feb02   0:00 [kswapd0]
root        26  0.0  0.0      0     0 ?        SN   Feb02   0:00 [ksmd]

选项:

a:列出与终端有关的所有process

u:列出进程对应的用户,即进程的发起者是谁。

x:与选项a连用,列出系统内所有的进程。

对输出的信息进行解释。

USER:进程的发起者。

PID:此进程的PID号。

%CPU:此进程在当前时刻占用的CPU百分比。即CPU利用率

%MEM:此进程在当前时刻占用的内存百分比。即内存利用率

VSZ:此进程使用的虚拟内存集大小,。注意这并不是swap分区,两者是有区别的。

RSS:此进程占用的固定内存大小。我们把这里面的进程称为常驻内存集。常驻内存集里面的进程是不能放在swap分区里面运行的。

TTY:进程在哪个终端运行

STAT:进程此时的运行状态。

START:进程是在哪一时刻运行的。

TIME:进程实际使用CPU的时间。

COMMAND:进程对应的命令。

2)进程的状态STAT都有什么?

R:running,表示此进程正在运行。

S:可中断睡眠。

D:不可中断睡眠。通常是指IO阻塞的进程。

T:停止运行。此时进程不可被调度,只能手动来进行启动。

Z:僵尸态, 孤魂野鬼,找不到归宿,父进程不来收尸或者父进程已经挂了。

不过我们看到的进程后面还有带有其他字母或字符,表示什么意思呢?

+:前台进程,占据着某终端。

s:session leader

l:(小写字母l)多线程进程

<:高优先级进程

N:低优先级进程。

3)自定义显示进程。(在shell脚本中多用)

选项“o”表示指定显示的进程信息。

比如:

[root@ELK-chaofeng tmp]# ps axo pid,command
  PID COMMAND
    1 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
    2 [kthreadd]
    3 [ksoftirqd/0]
    5 [kworker/0:0H]
    7 [migration/0]
    8 [rcu_bh]
    9 [rcu_sched]
   10 [watchdog/0]
   12 [kdevtmpfs]

4)ps命令还有一个常用组合,"ps axl",我们可以查看进程的PPID,PRI,NI等信息。

[root@ELK-chaofeng tmp]# ps axl
F   UID   PID  PPID PRI  NI    VSZ   RSS WCHAN  STAT TTY        TIME COMMAND
4     0     1     0  20   0  43144  3604 ep_pol Ss   ?          0:07 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
1     0     2     0  20   0      0     0 kthrea S    ?          0:00 [kthreadd]
1     0     3     2  20   0      0     0 smpboo S    ?          0:13 [ksoftirqd/0]
1     0     5     2   0 -20      0     0 worker S<   ?          0:00 [kworker/0:0H]
1     0     7     2 -100  -      0     0 smpboo S    ?          0:00 [migration/0]
1     0     8     2  20   0      0     0 rcu_gp S    ?          0:00 [rcu_bh]
1     0     9     2  20   0      0     0 -      R    ?          0:55 [rcu_sched]
5     0    10     2 -100  -      0     0 smpboo S    ?          0:02 [watchdog/0]
5     0    12     2  20   0      0     0 devtmp S    ?          0:00 [kdevtmpfs]
1     0    13     2   0 -20      0     0 rescue S<   ?          0:00 [netns]
1     0    14     2  20   0      0     0 watchd S    ?          0:00 [khungtaskd]
1     0    15     2   0 -20      0     0 rescue S<   ?          0:00 [writeback]
1     0    16     2   0 -20      0     0 rescue S<   ?          0:00 [kintegrityd]

加上选项小写字母l,表示长格式显示,显示的内容更全面了,

PRI:进程的优先级。数值越大,优先级越低,

5)UNIX风格我们习惯使用 “ps -eFH”。

UID        PID  PPID  C    SZ   RSS PSR STIME TTY          TIME CMD
root         2     0  0     0     0   0 Feb02 ?        00:00:00 [kthreadd]
root         3     2  0     0     0   0 Feb02 ?        00:00:13   [ksoftirqd/0]
root         5     2  0     0     0   0 Feb02 ?        00:00:00   [kworker/0:0H]
root         7     2  0     0     0   0 Feb02 ?        00:00:00   [migration/0]
root         8     2  0     0     0   0 Feb02 ?        00:00:00   [rcu_bh]
root         9     2  0     0     0   0 Feb02 ?        00:00:55   [rcu_sched]
root        10     2  0     0     0   0 Feb02 ?        00:00:02   [watchdog/0]
root         1     0  0 10786  3604   0 Feb02 ?        00:00:07 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
root       325     1  0  9204  3652   0 Feb02 ?        00:00:03   /usr/lib/systemd/systemd-journald
root       347     1  0 10939  2072   0 Feb02 ?        00:00:00   /usr/lib/systemd/systemd-udevd
polkitd    427     1  0 131878 11864  0 Feb02 ?        00:00:02   /usr/lib/polkit-1/polkitd --no-debug
root       429     1  0 187552 24656  0 Feb02 ?        00:13:26   /usr/local/cloudmonitor/CmsGoAgent.linux-amd64
root       430     1  0  6047  1708   0 Feb02 ?        00:00:03   /usr/lib/systemd/systemd-logind
dbus       438     1  0  6100  1636   0 Feb02 ?        00:00:06   /bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation
root       455     1  0  6460   944   0 Feb02 ?        00:00:00   /usr/sbin/atd -f
root       460     1  0 31563  1656   0 Feb02 ?        00:00:01   /usr/sbin/crond -n
root       493     1  0 27508   864   0 Feb02 ttyS0    00:00:00   /sbin/agetty --keep-baud 115200 38400 9600 ttyS0 vt220
root       494     1  0 27508   848   0 Feb02 tty1     00:00:00   /sbin/agetty --noclear tty1 linux
root       681     1  0 28219 12788   0 Feb02 ?        00:00:00   /sbin/dhclient -H ELK-chaofeng -1 -q -lf /var/lib/dhclient/dhclient--eth0.lease -pf /var/ru
root       740     1  0 62155  4788   0 Feb02 ?        00:00:01   /usr/sbin/rsyslogd -n
root       745     1  0 138286 16424  0 Feb02 ?        00:01:00   /usr/bin/python -Es /usr/sbin/tuned -l -P
ntp        760     1  0  7354  2100   0 Feb02 ?        00:00:02   /usr/sbin/ntpd -u ntp:ntp -g
root       872     1  0 28844  1728   0 Feb02 ?        00:00:00   /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/lo
mysql     2106   872  0 420449 253620 0 Feb02 ?        00:03:45     /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --
root       881     1  0  8181  4144   0 Feb02 ?        00:02:33   /usr/local/aegis/aegis_update/AliYunDunUpdate

这个我们可以查看父进程命令与子进程命令的关系。

3、pgrep命令详解。

此命令显示的内容我们在ps命令中可以查看到,因此不做过多的解释,我认为这个命令最大的好处就是可以写在shell脚本中。

1)pgrep命令多用来查看进程名对应的PID号

[root@ELK-chaofeng tmp]# pgrep httpd
2710
27266
[root@ELK-chaofeng tmp]# pgrep mysql
872
2106
9191

2)显示PID与进程名。

[root@ELK-chaofeng tmp]# pgrep -l httpd
2710 httpd
27266 httpd
[root@ELK-chaofeng tmp]# pgrep -l mysql
872 mysqld_safe
2106 mysqld
9191 mysql

3)显示某个用户启动了哪些进程。

[root@ELK-chaofeng tmp]# pgrep -U mysql
2106

4)一般上-U选项配合-l选项或者-a选项使用

[root@ELK-chaofeng tmp]# pgrep -U mysql -l
2106 mysqld
[root@ELK-chaofeng tmp]# pgrep -U mysql -a
2106 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/usr/local/mysql/data/ELK-chaofeng.err --open-files-limit=8192 --pid-file=/usr/local/mysql/data/ELK-chaofeng.pid --socket=/tmp/mysql.sock --port=3306

4、pidof命令

pidof命令多用来显示某个进程对应的PID号。

[root@ELK-chaofeng tmp]# pidof mysql
9191
[root@ELK-chaofeng tmp]# pidof httpd
27266 2710

这个命令我们可以在日常生活中使用,也可以在shell脚本中使用。

这里补充一下MySQL的进程名:MySQL服务器的进程名称是“mysqld”,并非“mysql”。而“mysql”是MySQL客户端的进程名。

总结个小技巧:查看某个用户是否存在:id USER。查看某个进程是否存在:pidof  PROGRAM 或者 pgrep PROGRAM。这个命令判断进程是否正常运行并不是100%准确,因为有时候服务虽然正常启动了并且进程号也存在。但是此服务的端口号不一定存在,此时服务启动仍然是失败的。

 4、top命令详解

top - 15:07:42 up 20:39,  2 users,  load average: 0.04, 0.04, 0.05
Tasks:  83 total,   2 running,  81 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.9 us,  0.9 sy,  0.0 ni, 98.1 id,  0.1 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1883724 total,  1247716 free,   371608 used,   264400 buff/cache
KiB Swap:        0 total,        0 free,        0 used.  1327100 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
    1 root      20   0   43240   3616   2420 S  0.0  0.2   0:01.67 systemd
    2 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kthreadd
    3 root      20   0       0      0      0 S  0.0  0.0   0:01.77 ksoftirqd/0

1)第一行解释

(系统当前时间),(系统从开始到现在的运行时间),(当前登录系统的用户数量),(系统1分钟、5分钟、15分钟内的平均负载)

我们比较关注最后一列,此列的值大于CPU的总核心数量时,就要引起注意了,说明此时CPU的负载较高。

2)第二行解释

这行主要显示系统的进程总数,以及进程的运行状态。比较关注的是zombie的数值,如果不为0,需要查看下哪个进程处于僵死状态。

3)第三行解释

这行显示CPU的负载信息。

us:用户空间,表示用户运行程序占用CPU的百分比。

sy:内核空间,表示运行内核程序占用CPU的百分比。

ni:nice,用户进程空间所改变过优先级的进程占用CPU的百分比。

id:空闲CPU的百分比。

wa:wait io,表示CPU等待I/O花费时间百分比。

hi:硬件中断,硬件中断占用CPU的百分比

si:软件中断,软件中断占用CPU的百分比

st:stolen,被虚拟机“偷走”的百分比。

4)第四行解释,

表示物理内存的使用情况。单位是Kb

total:表示物理内存的总大小。

free:表示空闲内存的空间大小。

used:当前使用内存的空间大小

buff/cache:缓冲区的空间大小。

5)第五行解释

这一行表示swap分区的使用情况

6)第六行解释

第六行默认是空白行,用于接收在top终端下用户输入的字符。主要有以下四个常用的:

P:大写字母,根据占据的CPU的百分比大小排序。

M:大写字母,根据占据的内存的百分比大小排序。

T:CPU累积占用的时间排序。这个是用来查看进程被CPU执行时间的总和。

k:终止指定进程。

7)第七行解释

第七行主要显示进程的信息。

这里介绍其中的一些

PR:进程的优先级。这个数值越小优先级越高

NI:nice值,与primary有关。也是值越小表示优先级越高。

VIRT:进程需要的虚拟内存大小,而非使用的虚拟内存大小。

RES;进程当前使用的内存大小,不包括swap

SHR:进程与其他进程共享的内存大小。

RES-SHR得出的结果表明进程所占用的物理内存大小。

S:进程的状态。

TIME+:CPU执行此进程的累积时间

5、free命令解释

free命令在不同系列的centos系统上的执行结果稍微有点小区别,这里我们讲解一下:

1)centos6系统上

$free -m
             total           used      free    shared    buffers     cached
Mem:         1002            769       233       0         62         421
-/+ buffers/cache:           286       716
Swap:         1153           0         1153

total = 第一行的used + 第一行的free

total = 第二行的used + 第二行的free

第一行的used这个数值表示当前系统使用的内存空间大小,这个数值包含了buffers和cached。

第二行的free = 第一行的free + 第一行的buffers + 第一行的cached

第二行中,- buffers/cache的大小表示被程序实实在在吃掉的内存,不可被挪用。而+ buffers/cache表示可以被挪用的内存总数。

关于swap分区的知识概念我们可以参考这个链接:https://blog.csdn.net/u011373710/article/details/70037649

在面试题中,会问到当前系统真正可用的实际内存大小是多少?这个计算方法就是直接查看“+buffers/cache”的数值。

2)centos7系统上

[root@ELK-chaofeng ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           1839         368        1206          32         264        1290
Swap:             0           0           0

total = used + free buff/cache

available = free + buff/cache - 不可被回收的内存(共享内存段、tmpfs、ramfs等)

在centos7系统中,used列就表示当前系统已经使用的内存空间大小。而available表示当前系统真正可用的实际内存空间大小。即系统可用内存,之前说过由于buffer和cache可以在需要时被释放回收,系统可用内存即 free + buffer + cache,在CentOS7之后这种说法并不准确,因为并不是所有的buffer/cache空间都可以被回收。

还有一点需要注意的是:centos6中的used的值包含了buffers和cached的空间。在centos7中,used的值不包含这两个数值的大小。

 6、htop命令解释

htop命令是top命令的plus工具。

[root@ELK-chaofeng ~]# htop

  CPU[||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||100.0%]   Tasks: 48, 223 thr; 1 running
  Mem[||||||||||||||||||||||||||                                416M/1.80G]   Load average: 0.10 0.06 0.07
  Swp[                                                               0K/0K]   Uptime: 22:02:33
                                                                              Mem[||||||||||||||||||||||||||                                416M/1.80G]

  PID USER      PRI  NI  VIRT   RES   SHR S CPU% MEM%   TIME+  Command
14129 root       20   0  119M  2460  1360 R 100.  0.1  0:00.01 htop
    1 root       20   0 43240  3616  2420 S  0.0  0.2  0:01.72 systemd --switched-root --system --deserialize 21
  326 root       20   0 36812  4240  3928 S  0.0  0.2  0:00.41 systemd-journald
  345 root       20   0 43536  1848  1288 S  0.0  0.1  0:00.02 systemd-udevd
  430 dbus       20   0 24400  1484  1176 S  0.0  0.1  0:00.80 dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation
  466 polkitd    20   0  515M 12036  4600 S  0.0  0.6  0:00.00 polkitd --no-debug
  468 polkitd    20   0  515M 12036  4600 S  0.0  0.6  0:00.21 polkitd --no-debug
  470 polkitd    20   0  515M 12036  4600 S  0.0  0.6  0:00.00 polkitd --no-debug
  472 polkitd    20   0  515M 12036  4600 S  0.0  0.6  0:00.00 polkitd --no-debug
  474 polkitd    20   0  515M 12036  4600 S  0.0  0.6  0:00.00 polkitd --no-debug
  443 polkitd    20   0  515M 12036  4600 S  0.0  0.6  0:00.37 polkitd --no-debug
  459 root       20   0  732M 20196  7468 S  0.0  1.1  0:06.14 CmsGoAgent.linux-amd64
  460 root       20   0  732M 20196  7468 S  0.0  1.1  0:00.00 CmsGoAgent.linux-amd64
  461 root       20   0  732M 20196  7468 S  0.0  1.1  0:00.00 CmsGoAgent.linux-amd64
  480 root       20   0  732M 20196  7468 S  0.0  1.1  0:19.82 CmsGoAgent.linux-amd64
  482 root       20   0  732M 20196  7468 S  0.0  1.1  0:00.00 CmsGoAgent.linux-amd64
 1569 root       20   0  732M 20196  7468 S  0.0  1.1  0:00.00 CmsGoAgent.linux-amd64
 1574 root       20   0  732M 20196  7468 S  0.0  1.1  0:27.90 CmsGoAgent.linux-amd64
 1577 root       20   0  732M 20196  7468 S  0.0  1.1  0:17.90 CmsGoAgent.linux-amd64
 1585 root       20   0  732M 20196  7468 S  0.0  1.1  0:25.13 CmsGoAgent.linux-amd64
  446 root       20   0  732M 20196  7468 S  0.0  1.1  1:36.96 CmsGoAgent.linux-amd64
  447 root       20   0 24188  1700  1392 S  0.0  0.1  0:00.40 systemd-logind
  449 root       20   0 25840   944   740 S  0.0  0.1  0:00.00 atd -f
  453 root       20   0  123M  1660   984 S  0.0  0.1  0:00.16 crond -n
  494 root       20   0  107M   852   724 S  0.0  0.0  0:00.00 agetty --noclear tty1 linux
  495 root       20   0  107M   860   736 S  0.0  0.0  0:00.00 agetty --keep-baud 115200 38400 9600 ttyS0 vt220
  682 root       20   0  110M 12784   312 S  0.0  0.7  0:00.00 dhclient -H ELK-chaofeng -1 -q -lf /var/lib/dhclient/dhclient--eth0.lease -pf /var/run/dhclien
 1161 root       20   0  540M 16424  5788 S  0.0  0.9  0:00.00 python -Es /usr/sbin/tuned -l -P
 1194 root       20   0  540M 16424  5788 S  0.0  0.9  0:00.00 python -Es /usr/sbin/tuned -l -P
 1202 root       20   0  540M 16424  5788 S  0.0  0.9  0:07.32 python -Es /usr/sbin/tuned -l -P
 1247 root       20   0  540M 16424  5788 S  0.0  0.9  0:00.00 python -Es /usr/sbin/tuned -l -P
  741 root       20   0  540M 16424  5788 S  0.0  0.9  0:07.44 python -Es /usr/sbin/tuned -l -P
  770 root       20   0  218M  7072  4292 S  0.0  0.4  0:00.16 rsyslogd -n
  771 root       20   0  218M  7072  4292 S  0.0  0.4  0:00.06 rsyslogd -n
  744 root       20   0  218M  7072  4292 S  0.0  0.4  0:00.24 rsyslogd -n
  763 ntp        20   0 29416  2104  1480 S  0.0  0.1  0:00.34 ntpd -u ntp:ntp -g
  855 root       20   0  112M  1712  1388 S  0.0  0.1  0:00.02 sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysq

这个工具的好处有以下三点:

1、可以显示某个用户启动了哪些进程

2、可以查看指定进程所打开的文件列表。

3、显示指定进程执行的系统调用。

1)查看某个用户启动的进程。

首先是我们在htop命令的终端下,按下“u”字符,然后在终端左侧使用上下键选择用户,最后按下enter键,就可以查看到指定用户启动的进程了。

 用这个命令查看用户的进程你会发现有很多,比如查看mysqld的进程。这时候我们不能很好的辨别哪个是mysql的主进程,因此我们在这个界面下按下F5键,这个主要是来显示进程树结构。此时会显示父进程与子进程之间的关系。

如下所示:

[root@ELK-chaofeng ~]# htop

  CPU[||                                                              2.3%]   Tasks: 55, 223 thr; 1 running
  Mem[||||||||||||||||||||||||||                                423M/1.80G]   Load average: 0.01 0.03 0.05
  Swp[                                                               0K/0K]   Uptime: 22:19:47
                                                                              Mem[||||||||||||||||||||||||||                                423M/1.80G]

  PID USER      PRI  NI  VIRT   RES   SHR S CPU% MEM%   TIME+  Command
 2431 mysql      20   0 1641M  243M 11876 S  0.0 13.2  0:27.84 │  └─ mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/loca
11433 mysql      20   0 1641M  243M 11876 S  0.0 13.2  0:00.05 │     ├─ mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/l
 3534 mysql      20   0 1641M  243M 11876 S  0.0 13.2  0:00.08 │     ├─ mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/l
 2993 mysql      20   0 1641M  243M 11876 S  0.0 13.2  0:00.00 │     ├─ mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/l
 2992 mysql      20   0 1641M  243M 11876 S  0.0 13.2  0:00.00 │     ├─ mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/l
 2959 mysql      20   0 1641M  243M 11876 S  0.0 13.2  0:00.00 │     ├─ mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/l
 2958 mysql      20   0 1641M  243M 11876 S  0.0 13.2  0:00.11 │     ├─ mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/l
 2957 mysql      20   0 1641M  243M 11876 S  0.0 13.2  0:00.15 │     ├─ mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/l
 2956 mysql      20   0 1641M  243M 11876 S  0.0 13.2  0:00.00 │     ├─ mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/l
 2929 mysql      20   0 1641M  243M 11876 S  0.0 13.2  0:00.00 │     ├─ mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/l
 2928 mysql      20   0 1641M  243M 11876 S  0.0 13.2  0:00.00 │     ├─ mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/l
 2927 mysql      20   0 1641M  243M 11876 S  0.0 13.2  0:00.00 │     ├─ mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/l
 2926 mysql      20   0 1641M  243M 11876 S  0.0 13.2  0:00.00 │     ├─ mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/l
 2925 mysql      20   0 1641M  243M 11876 S  0.0 13.2  0:02.78 │     ├─ mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/l
 2924 mysql      20   0 1641M  243M 11876 S  0.0 13.2  0:00.29 │     ├─ mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/l
 2923 mysql      20   0 1641M  243M 11876 S  0.0 13.2  0:11.08 │     ├─ mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/l
 2922 mysql      20   0 1641M  243M 11876 S  0.0 13.2  0:00.72 │     ├─ mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/l
 2568 mysql      20   0 1641M  243M 11876 S  0.0 13.2  0:01.59 │     ├─ mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/l
 2567 mysql      20   0 1641M  243M 11876 S  0.0 13.2  0:01.19 │     ├─ mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/l
 2566 mysql      20   0 1641M  243M 11876 S  0.0 13.2  0:00.80 │     ├─ mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/l

2)查看指定进程所打开的文件列表。

我们移动光标至某个进程,这里以mysqld进程为例,此时按下字符 l 键,我们就可以看到此进程打开了哪些文件。

3)显示指定进程执行的系统调用。

移动光标至某个进程,此时按下字符s键,就可以看到进行执行的系统调用。

7、vmstat命令详解

vmstat命令在以前的博文中介绍过,这里不再介绍,可以直接查看

8、dstat命令

dstat命令常用的选项是"-cmdlngy"。比如“dstat -cmdlngy”。显示的格式与vmstat命令的格式差不多一致。

不过我经过几次测试发现dstat显示的是一种大概值,并不是特别精确的数值。因此这个命令用来排查问题用的不多。我自己使用这个命令主要是查看三项内容

1)查看最消耗CPU的进程

[root@ELK-chaofeng ~]# dstat --top-cpu
-most-expensive-
  cpu process
staragent-cor1.2
staragent-cor2.0

2)查看最消耗内存的进程

[root@ELK-chaofeng ~]# dstat --top-mem
--most-expensive-
  memory process
mysqld       243M

3)查看最消耗IO的进程

[root@ELK-chaofeng ~]# dstat --top-io
----most-expensive----
     i/o process
bash       9779B 9674B
CmsGoAgent.2202B    0
staragent-p  30k    0
CmsGoAgent.2202B    0
CmsGoAgent.2202B    0

9、kill命令详解

我自己经常使用kill -9 PROGRAM命令来杀死进程,那么为什么要指定-9参数呢?

kill -l命令可以显示Linux系统中可用的信号

[root@ELK-chaofeng ~]# kill -l
 1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL       5) SIGTRAP
 6) SIGABRT      7) SIGBUS       8) SIGFPE       9) SIGKILL     10) SIGUSR1
11) SIGSEGV     12) SIGUSR2     13) SIGPIPE     14) SIGALRM     15) SIGTERM
16) SIGSTKFLT   17) SIGCHLD     18) SIGCONT     19) SIGSTOP     20) SIGTSTP
21) SIGTTIN     22) SIGTTOU     23) SIGURG      24) SIGXCPU     25) SIGXFSZ
26) SIGVTALRM   27) SIGPROF     28) SIGWINCH    29) SIGIO       30) SIGPWR
31) SIGSYS      34) SIGRTMIN    35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3
38) SIGRTMIN+4  39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7
58) SIGRTMAX-6  59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
63) SIGRTMAX-1  64) SIGRTMAX

信号的三种表示方式:

1、完整名称,比如上面显示的完整名称。例如:SIGKILL

2、简写名称,例如KILL。实际上简写名称就是去掉前面的SIG三个字符

3、数据代称,就是数字,比如SIGKILL对应数值9。

我们可以使用这三种表示法来杀死进程:

[root@ELK-chaofeng ~]# kill -KILL 16217
-bash: kill: (16217) - No such process
[root@ELK-chaofeng ~]# kill -9 16217
-bash: kill: (16217) - No such process
[root@ELK-chaofeng ~]# kill -SIGKILL 16217
-bash: kill: (16217) - No such process

上面的信号非常多,下面列举几个我们常用的信号

SIGHUP:1,通知进程重读配置文件以让新的配置生效,无需重新启动进程。这个在服务器领域用的最多,在不关掉服务的情况下重新读取配置文件。比如nginx服务:nginx -s reload用的就是这个原理。

SIGINT:2,打断正在运行中的进程,相当于键盘组合键Ctrl+c

SIGKILL:9,强行终止正在运行中的进程

SIGTERM:15,安全终止正在运行中的进程。

SIGSTOP:19,暂停进程。

SIGCONT:18,继续运行指定进程。

10、killall命令

比如我们想杀死一个进程,那么我们需要先使用ps命令得到这个进程的PID号,然后使用kill命令杀死此进程。这样一来比较麻烦,我们可以使用killall命令直接杀死进程。比如 killal PROGRAM

[root@ELK-chaofeng ~]# ps aux | grep mysqld
root       855  0.0  0.0 115380  1772 ?        S    Feb10   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/ELK-chaofeng.pid
mysql    16280  0.0 11.9 1680268 224300 ?      Sl   17:29   0:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/usr/local/mysql/data/ELK-chaofeng.err --open-files-limit=8192 --pid-file=/usr/local/mysql/data/ELK-chaofeng.pid --socket=/tmp/mysql.sock --port=3306
root     16644  0.0  0.0 112644   968 pts/4    R+   17:39   0:00 grep --color=auto mysqld
[root@ELK-chaofeng ~]# killall mysqld
[root@ELK-chaofeng ~]# ps aux | grep mysqld
root     16661  0.0  0.0 112644   964 pts/4    R+   17:39   0:00 grep --color=auto mysqld

猜你喜欢

转载自www.cnblogs.com/FengGeBlog/p/10353275.html
今日推荐