Kiểm tra service chết trên linux

Chúng ta dùng các lệnh linux có thể kiểm tra tình trạng bộ nhớ sử dụng thế nào. Bộ nhớ gồm có RAM và lưu tạm [swap]. Kiểm tra bộ nhớ là quan trọng để biết server sử dụng tài nguyên như thế nào. 

Lệnh free

Lệnh free cho biết thông tin tổng số, số đã sử dụng của bộ nhớ vật lý và bộ nhớ swap trên hệ thống, cũng như bộ đệm và các cache, thông tin này thực chất nó thu thập từ file /pro/meminfo

swap là bộ nhớ do hệ thống Linux tạo ra trên ổ đĩa, nó sử dụng nhơ bộ nhớ vật lý để đề phòng khi bộ nhớ vật lý đầy thì dùng swap.

Đây là lệnh đơn giản vè dễ dùng để kiểm tra bộ nhớ.

$ free -h
              total        used        free      shared  buff/cache   available
Mem:           7.6G        5.7G        1.4G         94M        554M        1.5G
Swap:          7.9G        1.0G        6.8G

Với tùy chọn -h để dữ liệu hiện thị ở dạng dễ đọc, -m các số liệu sẽ là

$ vmstat -s -SM
         7820 M total memory
         5620 M used memory
         3889 M active memory
         1633 M inactive memory
         1633 M free memory
            0 M buffer memory
          567 M swap cache
         8063 M total swap
         1069 M used swap
         6994 M free swap
    605125725 non-nice user cpu ticks
          935 nice user cpu ticks
     65688845 system cpu ticks
   8522280247 idle cpu ticks
      5297010 IO-wait cpu ticks
            0 IRQ cpu ticks
      3259077 softirq cpu ticks
            0 stolen cpu ticks
     41871348 pages paged in
    290981077 pages paged out
        23525 pages swapped in
       355631 pages swapped out
    774148951 interrupts
    107517783 CPU context switches
   1578562406 boot time
     13808720 forks     
0

Các cột dữ liệu:

  • $ vmstat -s -SM
             7820 M total memory
             5620 M used memory
             3889 M active memory
             1633 M inactive memory
             1633 M free memory
                0 M buffer memory
              567 M swap cache
             8063 M total swap
             1069 M used swap
             6994 M free swap
        605125725 non-nice user cpu ticks
              935 nice user cpu ticks
         65688845 system cpu ticks
       8522280247 idle cpu ticks
          5297010 IO-wait cpu ticks
                0 IRQ cpu ticks
          3259077 softirq cpu ticks
                0 stolen cpu ticks
         41871348 pages paged in
        290981077 pages paged out
            23525 pages swapped in
           355631 pages swapped out
        774148951 interrupts
        107517783 CPU context switches
       1578562406 boot time
         13808720 forks     
    
    1 : tổng số bộ nhớ
  • $ vmstat -s -SM
             7820 M total memory
             5620 M used memory
             3889 M active memory
             1633 M inactive memory
             1633 M free memory
                0 M buffer memory
              567 M swap cache
             8063 M total swap
             1069 M used swap
             6994 M free swap
        605125725 non-nice user cpu ticks
              935 nice user cpu ticks
         65688845 system cpu ticks
       8522280247 idle cpu ticks
          5297010 IO-wait cpu ticks
                0 IRQ cpu ticks
          3259077 softirq cpu ticks
                0 stolen cpu ticks
         41871348 pages paged in
        290981077 pages paged out
            23525 pages swapped in
           355631 pages swapped out
        774148951 interrupts
        107517783 CPU context switches
       1578562406 boot time
         13808720 forks     
    
    2 : bộ nhớ đã dùng - nó được tính bằng cách lấy tổng bộ nhớ trừ đi bộ nhớ còn trống, đệm và cache
  • free : bộ nhớ còn trống [chưa dùng đến]
  • $ vmstat -s -SM
             7820 M total memory
             5620 M used memory
             3889 M active memory
             1633 M inactive memory
             1633 M free memory
                0 M buffer memory
              567 M swap cache
             8063 M total swap
             1069 M used swap
             6994 M free swap
        605125725 non-nice user cpu ticks
              935 nice user cpu ticks
         65688845 system cpu ticks
       8522280247 idle cpu ticks
          5297010 IO-wait cpu ticks
                0 IRQ cpu ticks
          3259077 softirq cpu ticks
                0 stolen cpu ticks
         41871348 pages paged in
        290981077 pages paged out
            23525 pages swapped in
           355631 pages swapped out
        774148951 interrupts
        107517783 CPU context switches
       1578562406 boot time
         13808720 forks     
    
    4 : bộ nhớ dùng bởi tmpfs
  • $ vmstat -s -SM
             7820 M total memory
             5620 M used memory
             3889 M active memory
             1633 M inactive memory
             1633 M free memory
                0 M buffer memory
              567 M swap cache
             8063 M total swap
             1069 M used swap
             6994 M free swap
        605125725 non-nice user cpu ticks
              935 nice user cpu ticks
         65688845 system cpu ticks
       8522280247 idle cpu ticks
          5297010 IO-wait cpu ticks
                0 IRQ cpu ticks
          3259077 softirq cpu ticks
                0 stolen cpu ticks
         41871348 pages paged in
        290981077 pages paged out
            23525 pages swapped in
           355631 pages swapped out
        774148951 interrupts
        107517783 CPU context switches
       1578562406 boot time
         13808720 forks     
    
    5 : bộ nhớ đệm [lưu trữ tạm thời dữ liệu trong khi đọc/ghi từ nguồn bên ngoài - như đĩa]

tempfs còn gọi là

$ vmstat -s -SM
         7820 M total memory
         5620 M used memory
         3889 M active memory
         1633 M inactive memory
         1633 M free memory
            0 M buffer memory
          567 M swap cache
         8063 M total swap
         1069 M used swap
         6994 M free swap
    605125725 non-nice user cpu ticks
          935 nice user cpu ticks
     65688845 system cpu ticks
   8522280247 idle cpu ticks
      5297010 IO-wait cpu ticks
            0 IRQ cpu ticks
      3259077 softirq cpu ticks
            0 stolen cpu ticks
     41871348 pages paged in
    290981077 pages paged out
        23525 pages swapped in
       355631 pages swapped out
    774148951 interrupts
    107517783 CPU context switches
   1578562406 boot time
     13808720 forks     
6 hay
$ vmstat -s -SM
         7820 M total memory
         5620 M used memory
         3889 M active memory
         1633 M inactive memory
         1633 M free memory
            0 M buffer memory
          567 M swap cache
         8063 M total swap
         1069 M used swap
         6994 M free swap
    605125725 non-nice user cpu ticks
          935 nice user cpu ticks
     65688845 system cpu ticks
   8522280247 idle cpu ticks
      5297010 IO-wait cpu ticks
            0 IRQ cpu ticks
      3259077 softirq cpu ticks
            0 stolen cpu ticks
     41871348 pages paged in
    290981077 pages paged out
        23525 pages swapped in
       355631 pages swapped out
    774148951 interrupts
    107517783 CPU context switches
   1578562406 boot time
     13808720 forks     
7, là một loại ổ đĩa lấy Ram để tạo ra nhằm có ổ đĩa mà tốc độ đọc ghi rất nhanh, các file lưu vào đây mất khi máy khởi động lại.

Linux có xu hướng caching nhiều thứ để tăng tốc, vì đó mà

$ vmstat -s -SM
         7820 M total memory
         5620 M used memory
         3889 M active memory
         1633 M inactive memory
         1633 M free memory
            0 M buffer memory
          567 M swap cache
         8063 M total swap
         1069 M used swap
         6994 M free swap
    605125725 non-nice user cpu ticks
          935 nice user cpu ticks
     65688845 system cpu ticks
   8522280247 idle cpu ticks
      5297010 IO-wait cpu ticks
            0 IRQ cpu ticks
      3259077 softirq cpu ticks
            0 stolen cpu ticks
     41871348 pages paged in
    290981077 pages paged out
        23525 pages swapped in
       355631 pages swapped out
    774148951 interrupts
    107517783 CPU context switches
   1578562406 boot time
     13808720 forks     
8thường xuyên được giải phóng, lưu tạm vào đĩa và sử dụng nếu cần.

Lệnh vmstat

$ vmstat -s -SM
         7820 M total memory
         5620 M used memory
         3889 M active memory
         1633 M inactive memory
         1633 M free memory
            0 M buffer memory
          567 M swap cache
         8063 M total swap
         1069 M used swap
         6994 M free swap
    605125725 non-nice user cpu ticks
          935 nice user cpu ticks
     65688845 system cpu ticks
   8522280247 idle cpu ticks
      5297010 IO-wait cpu ticks
            0 IRQ cpu ticks
      3259077 softirq cpu ticks
            0 stolen cpu ticks
     41871348 pages paged in
    290981077 pages paged out
        23525 pages swapped in
       355631 pages swapped out
    774148951 interrupts
    107517783 CPU context switches
   1578562406 boot time
     13808720 forks     
9 cho thông tin về bộ nhớ, các tiến trình, hoạt động của CPU, ổ đĩa. Trong đó có thông tin về bộ nhớ [tổng, đã dùng]

$ vmstat -s -SM
         7820 M total memory
         5620 M used memory
         3889 M active memory
         1633 M inactive memory
         1633 M free memory
            0 M buffer memory
          567 M swap cache
         8063 M total swap
         1069 M used swap
         6994 M free swap
    605125725 non-nice user cpu ticks
          935 nice user cpu ticks
     65688845 system cpu ticks
   8522280247 idle cpu ticks
      5297010 IO-wait cpu ticks
            0 IRQ cpu ticks
      3259077 softirq cpu ticks
            0 stolen cpu ticks
     41871348 pages paged in
    290981077 pages paged out
        23525 pages swapped in
       355631 pages swapped out
    774148951 interrupts
    107517783 CPU context switches
   1578562406 boot time
     13808720 forks     

Những dòng trên cùng cho biết tổng bộ nhớ, bộ nhớ trống ...

Lệnh top

Lệnh top thường được dùng để kiểm tra tình trạng sử dụng bộ nhớ và CPU cho các process. Tuy vậy nó cũng cho biết và giám sát tình trạng sử dụng RAM. Lệnh này nó thống kê liên tục [mặc định 3s thông báo 1 lần], để kết thúc lệnh gõ Ctrl + C

top - 09:05:12 up 133 days, 16:31,  2 users,  load average: 0.73, 0.72, 0.61
Tasks: 266 total,   1 running, 265 sleeping,   0 stopped,   0 zombie
%Cpu[s]:  0.7 us,  0.4 sy,  0.0 ni, 98.9 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  8008448 total,  1713752 free,  5713144 used,   581552 buff/cache
KiB Swap:  8257532 total,  7163084 free,  1094448 used.  1893040 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
 3690 33        20   0  184696  26692   7984 S   5.0  0.3   1427:47 apache2
 3597 root      20   0   11.3g   3.8g   3664 S   2.3 49.8  97178:35 sqlservr
 2722 polkitd   20   0 3134152 640808   3024 S   0.7  8.0   2790:23 mysqld
13365 root      20   0  162152   2416   1572 R   0.7  0.0   0:00.20 top
29870 root      20   0 3264612  24828   5680 S   0.7  0.3  84:32.49 dotnet
 1483 root      20   0 1456792  43820   2232 S   0.3  0.5 484:19.80 dockerd
    1 root      20   0  193732   4140   2304 S   0.0  0.1   4:30.74 systemd
    2 root      20   0       0      0      0 S   0.0  0.0   0:05.18 kthreadd

Thông tin trong /proc/meminfo

File

top - 09:05:12 up 133 days, 16:31,  2 users,  load average: 0.73, 0.72, 0.61
Tasks: 266 total,   1 running, 265 sleeping,   0 stopped,   0 zombie
%Cpu[s]:  0.7 us,  0.4 sy,  0.0 ni, 98.9 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  8008448 total,  1713752 free,  5713144 used,   581552 buff/cache
KiB Swap:  8257532 total,  7163084 free,  1094448 used.  1893040 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
 3690 33        20   0  184696  26692   7984 S   5.0  0.3   1427:47 apache2
 3597 root      20   0   11.3g   3.8g   3664 S   2.3 49.8  97178:35 sqlservr
 2722 polkitd   20   0 3134152 640808   3024 S   0.7  8.0   2790:23 mysqld
13365 root      20   0  162152   2416   1572 R   0.7  0.0   0:00.20 top
29870 root      20   0 3264612  24828   5680 S   0.7  0.3  84:32.49 dotnet
 1483 root      20   0 1456792  43820   2232 S   0.3  0.5 484:19.80 dockerd
    1 root      20   0  193732   4140   2304 S   0.0  0.1   4:30.74 systemd
    2 root      20   0       0      0      0 S   0.0  0.0   0:05.18 kthreadd
0 do Linux tạo ra nó nằm trong RAM, nó chứa thông tin cập nhật về bộ nhớ

Để đọc nội dung có thể sử dụng

top - 09:05:12 up 133 days, 16:31,  2 users,  load average: 0.73, 0.72, 0.61
Tasks: 266 total,   1 running, 265 sleeping,   0 stopped,   0 zombie
%Cpu[s]:  0.7 us,  0.4 sy,  0.0 ni, 98.9 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  8008448 total,  1713752 free,  5713144 used,   581552 buff/cache
KiB Swap:  8257532 total,  7163084 free,  1094448 used.  1893040 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
 3690 33        20   0  184696  26692   7984 S   5.0  0.3   1427:47 apache2
 3597 root      20   0   11.3g   3.8g   3664 S   2.3 49.8  97178:35 sqlservr
 2722 polkitd   20   0 3134152 640808   3024 S   0.7  8.0   2790:23 mysqld
13365 root      20   0  162152   2416   1572 R   0.7  0.0   0:00.20 top
29870 root      20   0 3264612  24828   5680 S   0.7  0.3  84:32.49 dotnet
 1483 root      20   0 1456792  43820   2232 S   0.3  0.5 484:19.80 dockerd
    1 root      20   0  193732   4140   2304 S   0.0  0.1   4:30.74 systemd
    2 root      20   0       0      0      0 S   0.0  0.0   0:05.18 kthreadd
1

cat /proc/meminfo

MemTotal:        8008448 kB
MemFree:         1743488 kB
MemAvailable:    1923764 kB
Buffers:               4 kB
Cached:           323496 kB
SwapCached:        49920 kB
Active:          3920464 kB
Inactive:        1663112 kB
Active[anon]:    3844908 kB
Inactive[anon]:  1511520 kB
Active[file]:      75556 kB
Inactive[file]:   151592 kB

Lệnh htop

Tương tự như top, nhưng trực quan và có nhiều thiết lập xem chi tiết hơn. htop có sẵn trong Ubuntu, CentOS thì cài thêm bằng lệnh

Chủ Đề