1. 程式人生 > >每周一個linux命令之---uptime詳解

每周一個linux命令之---uptime詳解

big ber 三個時間 load little 如果 ESS p s minutes

每周一個linux命令之---uptime詳解

linux命令 uptime詳解


引言:從今天開始,每周更新一個對程序員有用的linux命令,我真的沒敢寫每天一個,我怕我堅持不下去,每周一個還是可以的。既然每周一個了,就肯定不能拿ls,chmod這種命令來湊數了。降低數量,保障質量。與大家共勉。

進入正題

uptime

我們先來看一下命令執行結果

11:10:42 up 4 days, 19:23,  2 users,  load average: 3.17, 3.45, 3.65

我們來分組解釋一下,大概分成一下幾個部分

  • 系統當前時間 11:10:42
  • up 4 days, 19:23 從上次啟動開始系統運行的時間
  • 2 users 註意這裏實際是連接數量,可以自己測試出來,同一用戶多個連接的時候算多個
  • load average: 3.17, 3.45, 3.65 這是重頭戲,分別描述了1分鐘5分鐘15分鐘內系統平均負載

我們使用man uptime命令進行查詢uptime的說明如下(man命令不了解的稍微百度一下就明白,這是一個類似手冊的功能)

註:一般我們使用 uptime -h 即命令加-h或--help的形式得到的是比較簡單的幫助文檔,而man命令一般會給出更詳細的解釋,不過一般都是英文的,其實也沒有太難的詞語希望大家堅持英文閱讀。提高技術水平的同時也潛移默化的提高了英語水平

uptime  gives  a  one  line display of the following information.
       The current time, how long the system has been running, how  many
       users  are  currently logged on, and the system load averages for
       the past 1, 5, and 15 minutes.

       This is the same information contained in the  header  line  dis‐
       played by w(1).

       System  load averages is the average number of processes that are
       either in a runnable or uninterruptable state.  A  process  in  a
       runnable state is either using the CPU or waiting to use the CPU.
       A process in  uninterruptable  state  is  waiting  for  some  I/O
       access,  eg  waiting  for  disk.  The averages are taken over the
       three time intervals.  Load averages are not normalized  for  the
       number of CPUs in a system, so a load average of 1 means a single
       CPU system is loaded all the time while on  a  4  CPU  system  it
       means it was idle 75% of the time.

當然我知道大部分童鞋是不想看那麽長串的英文的,我翻譯了一下如下:

uptime 在一行中給了如下信息:當前時間,系統已經運行了多久,多少用戶連接目前正在使用系統,系統在過去1,5,15分鐘內的平均負載。
這與w(1)顯示的標題行中包含相同的信息。
系統負載平均值是處於可運行或不可中斷狀態的平均進程數。 進程在可運行狀態是指進程正在使用CPU或等待使用CPU。 處於不可中斷狀態的進程是指的進程正在等待某些I/O訪問,例如等待磁盤。 平均值是在三個時間間隔內取得的。 對於系統中的CPU數量,負載平均值沒有標準化,因此負載平均值為1意味著單個CPU系統一直在加載,而在4 CPU系統上,這意味著它在75%的時間內處於空閑狀態。

這裏我們做一個比喻便於理解,我們把處於可運行或不可中斷狀態的的平均進程數量比喻成貨物數量,而cpu可以理解為傳送帶。貨物數量是1個的話,如果只有一條傳送帶,那麽傳送帶就是滿負荷運行,如果有4個傳送帶,那麽就有三條空閑(即上文說的75%空閑)。平均負載就可以理解為一段時間內的平均貨物數量。

接下來我們學習一下uptime可攜帶的參數

OPTIONS
       -p, --pretty
              show uptime in pretty format//以比較友好的格式輸出

       -h, --help
              display this help text//顯示幫助選項

       -s, --since
              system up since, in yyyy-mm-dd HH:MM:SS format//系統啟動時間

       -V, --version
              display version information and exit//版本信息

運行結果如下


@dev1:~$ uptime -p
up 4 days, 22 hours, 38 minutes
@dev1:~$ uptime -s
2018-09-13 15:47:04
@dev1:~$ uptime -V
uptime from procps-ng 3.3.10
@dev1:~$ uptime -h

Usage:
 uptime [options]

Options:
 -p, --pretty   show uptime in pretty format
 -h, --help     display this help and exit
 -s, --since    system up since
 -V, --version  output version information and exit

For more details see uptime(1).

下面順便提一下怎麽查系統的cpu核心數量。(方法有很多種,我覺得這種最好記憶)

lscpu

這個命令應該是比較好記的,畢竟ls,cpu這兩個我們都記得了。
這裏面顯示了很多選項。其中cpu(s):數量表明了cpu的個數。如下圖

Architecture:          x86_64 //cpu架構
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian//小端(低位字節存儲在內存的低地址端 intel的cpu一般是小端,amd的一般是大端 Big Endian)
CPU(s):                4//cpu數量

其他參數以後再講吧。一篇文章太長了不利於學習和記憶。

每周一個linux命令之---uptime詳解