Linux運維平台工具:pstree、ps、top、htop、free、vmstat、dstat、kill、killall、ipcalc


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

 11、ipcalc命令

我個人使用ipcalc命令用於檢測腳本傳值過來的ip地址是否合法

[root@master01 ~]# ipcalc -c 192.174.241.6 &>/dev/null; [ $? -eq 0 ] && echo "This is the correct ip address" || echo "This is not the correct ip address" This is the correct ip address
[root@master01 ~]# ipcalc -c 192.774.241.6 &>/dev/null; [ $? -eq 0 ] && echo "This is the correct ip address" || echo "This is not the correct ip address"
This is not the correct ip address


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM