1. 程式人生 > >Linux--每天學習一個命令(一)

Linux--每天學習一個命令(一)

Linux 系統監控

Top命令

Processes: 355 total, 3 running, 352 sleeping, 1540 threads            22:53:50
Load Avg: 2.23, 1.85, 1.79  CPU usage: 2.91% user, 3.4% sys, 94.3% idle
SharedLibs: 203M resident, 47M data, 29M linkedit.
MemRegions: 67938 total, 2480M resident, 96M private, 956M shared.
PhysMem: 9029M used (3177M wired), 7345M unused.
VM: 995G vsize, 627M framework vsize, 66673141(0) swapins, 67456042(0) swapouts.
Networks: packets: 10075546/9110M in, 7870367/1935M out.
Disks: 5209393/349G read, 3216109/340G written.

PID    COMMAND      %CPU TIME     #TH    #WQ  #PORT MEM    PURG   CMPRS  PGRP
36055  git          0.0  00:00.06 1      0    10    928K   0B     0B     35626
36054  git          0.0  00:01.33 2      0    11    7628K  0B     0B     35626
36052  mdworker     0.0  00:00.06 4      1    48    5224K  0B     0B     36052
36051  mdworker     0.0  00:00.03 3      1    46    2932K  0B     0B     36051
36050  mdworker     0.0  00:00.06 4      1    48    8084K  0B     0B     36050
36049  top          2.8  00:06.87 1/1    0    25    3400K  0B     0B     36049
35964  bash         0.0  00:00.07 1      0    16    2668K  0B     0B     35964
35963  login        0.0  00:00.02 2      1    29    1132K  0B     0B     35963
35961  Google Chrom 0.0  00:05.98 18     2    156   77M    0B     0B     4804
35955  git-remote-h 0.1  00:00.29 2      1    37    10M    0B     0B     35626
35954  git          0.0  00:00.02 1      0    10    6932K  0B     0B     35626
35949  git-remote-h 0.1  00:00.26 2      1    37    4672K+ 0B     0B     35626
35948  git          0.0  00:00.01 1      0    10    968K   0B     0B     35626
35940  bash         0.0  00:00.00 1      0    8     460K   0B     0B     35626

然而還有個好用的工具那就是 glances(驚鴻一瞥)

mac下載方法:

brew install -y glances

Linux Ubuntu:

apt install -y glances

Linux Centos:

yum install -y glances

Pip:

pip install glances

glances使用方法:

在終端直接敲glances:

CPU[9.3%]CPU / 9.3%nice: 0.0%ctx_sw: 441MEM - 61.2%SWAP - 52.6%LOAD8-core

MEM[ 61.3%]user: 6.7%inter:-2515total:16.0Gtotal: 2.00G

1 min:1.69

SWAP [ 52.6%]system: 2.7%sw_int: 446used: 9.80Gused: 1.05G5 min:1.72

idle:90.7%syscal: 0free: 6.20Gfree: 970M15 min: 1.73

NETWORK Rx/s Tx/sTASKS 195 (939 thr), 195 run, 0 slp, 0 oth sorted automatically by cpu_percent, flat view

awdl0 0b 0b

bridge0 0b 0bCPU%MEM%VIRT RES PID USERNI S TIME+ Command

en02Kb 864b39.3 0.7

3.45G121M 293 yuqibing 0 R11:12.56 /Applications/Utilities/Terminal.

en1 0b 0b 4.9 0.9 4.21G155M 18921 yuqibing 0 R47:40.92 /Applications/Google Chrome.app/C

en2 0b 0b 2.9 0.1 2.44G 13.1M4811 yuqibing 0 R22:06.99 /System/Library/Frameworks/VideoT

lo0 280b 280b 2.7 0.3 2.41G 43.9M 38637 yuqibing 0 R 0:03.34 /Library/Frameworks/Python.framew

p2p00b 0b 2.4 1.2 9.44G190M4809 yuqibing 0 R 1h10:32 /Applications/Google Chrome.app/C

utun0 0b 0b 1.4 1.7 5.19G276M4804 yuqibing 0 R 2h05:54 /Applications/Google Chrome.app/C

utun1 0b 0b 1.2 0.7 2.96G115M 370 yuqibing 0 R 7:49.55 /System/Library/Input Methods/SCI

0.5 2.3 4.04G383M 19198 yuqibing 0 R 3:53.39 /Applications/Google Chrome.app/C

DISK I/O R/sW/s 0.3 0.6 3.63G103M 35137 yuqibing 0 R 0:42.80 /Applications/Google Chrome.app/C

disk0 494K0 0.1 0.2 3.55G 34.1M 18920 yuqibing 0 R 1:15.87 /Applications/Google Chrome.app/C

disk200 0.1 0.5 3.70G 83.3M 18944 yuqibing 0 R 3:57.14 /Applications/Google Chrome.app/C

disk300 0.1 0.5 3.67G 90.0M 19058 yuqibing 0 R 1:23.48 /Applications/Google Chrome.app/C

0.1 0.1 4.48G 10.1M 19543 yuqibing 0 R 0:54.79 /Library/Java/JavaVirtualMachines

FILE SYS UsedTotal 0.1 0.2 3.57G 25.7M 19560 yuqibing 0 R 0:52.25 /Applications/Google Chrome.app/C

/ (disk1) 115G 233G 0.1 0.6 3.39G106M 35625 yuqibing 0 R 0:01.54 /Applications/Google Chrome.app/C

_sX-NG-R818.0M22.4M 0.0 0.4 2.67G 72.3M 111 root 0 R 2:18.93 /System/Library/CoreServices/logi

_CCB6B9D7 118G 233G 0.0 0.1 2.41G 13.4M 278 yuqibing 0 R 0:54.79 /usr/sbin/cfprefsd agent

0.0 0.1 2.43G 20.3M 279 yuqibing 0 R 1:11.77 /usr/libexec/UserEventAgent (Aqua

SENSORS 0.0 0.1 2.40G 11.8M 281 yuqibing 0 R 2:58.90 /usr/sbin/distnoted agent

Battery 46% 0.0 0.2 2.45G 25.4M 283 yuqibing 0 R 0:49.88 /System/Library/Frameworks/CoreTe

0.0 0.1 2.43G 16.4M 284 yuqibing 0 R 0:12.73 /usr/libexec/lsd

0.0 0.1 3.46G 18.3M 285 yuqibing 0 R 1:55.30 /usr/libexec/trustd --agent

0.0 0.1 2.42G 13.4M 289 yuqibing 0 R 0:06.44 /System/Library/CoreServices/shar

Glances 會用一下幾種顏色來代表狀態:

  • 綠色:OK(一切正常)
  • 藍色:CAREFUL(需要注意)
  • 紫色:WARNING(警告)
  • 紅色:CRITICAL(嚴重)

Glances 的選項:

  • a – 對程序自動排序
  • c – 按 CPU 百分比對程序排序
  • m – 按記憶體百分比對程序排序
  • p – 按程序名字母順序對程序排序
  • i – 按讀寫頻率(I/O)對程序排序
  • d – 顯示/隱藏磁碟 I/O 統計資訊
  • f – 顯示/隱藏檔案系統統計資訊
  • n – 顯示/隱藏網路介面統計資訊
  • s – 顯示/隱藏感測器統計資訊
  • y – 顯示/隱藏硬碟溫度統計資訊
  • l – 顯示/隱藏日誌(log)
  • b – 切換網路 I/O 單位(Bytes/bits)
  • w – 刪除警告日誌
  • x – 刪除警告和嚴重日誌
  • 1 – 切換全域性 CPU 使用情況和每個 CPU 的使用情況
  • h – 顯示/隱藏這個幫助畫面
  • t – 以組合形式瀏覽網路 I/O
  • u – 以累計形式瀏覽網路 I/O
  • q – 退出(‘ESC‘ 和 ‘Ctrl&C‘ 也可以)

更多glances的選項,可以用

man glances來參看。

遠端使用 Glances

glances -s

Define the password for the Glances server
Password: 
Password (confirm): 
Glances server is running on 0.0.0.0:61209

當你執行了glances -s命令後,會讓你為 Glances 伺服器設定密碼。當你設定完畢,你將看到 “Glances server is running on 0.0.0.0:61209” (Glances 伺服器正在 0.0.0.0 的 61209 埠執行)的訊息。當 Glances 伺服器啟動後,到本地執行下面的命令來指定伺服器IP地址或主機名以連結。

注:這裡的172.16.27.56是我 Glances 伺服器的 IP 地址。glances-c-P 172.16.27.56

下面是一些在使用伺服器/客戶端模式時必須知道的事情:

  • 在伺服器模式,你可以通過 -B 地址 來設定繫結地址,也可以通過 -p 埠 來繫結監聽的 TCP 埠
  • 在客戶端模式,你可以通過同樣的 -p 埠 來指定伺服器埠
  • 預設的繫結地址是 0.0.0.0,但這麼做會監聽所有網路介面的指定埠
  • 在伺服器/客戶端模式下,限制的閥值將由伺服器的設定決定
  • 你也可以在命令列下用過 -P 密碼 的方式來為伺服器端設定一個密碼

pmap

可以根據程序檢視程序相關資訊佔用的記憶體情況,(程序號可以通過ps檢視)如下所示:

  $ pmap -d 5647

Linux中使用free來檢視記憶體

  $ free

             total       used       free     shared    buffers     cached
Mem:       8193564    7868388     325176          0     198848    2645932
-/+ buffers/cache:    5023608    3169956
Swap:            0          0          0


如何看額定值:

cat /proc/meminfo

proc:

/proc是Linux的一個偽檔案系統,儲存的是當前核心執行狀態的一系列特殊檔案,使用者可以通過這些檔案檢視有關係統硬體及當前正在執行程序的資訊,甚至可以通過更改其中某些檔案來改變核心的執行狀態。 

/proc/cpuinfo   cpu的相關資訊