網站搜尋

Tuptime - 一個用來查找 Linux 系統運行時間的命令列工具


通常,我們使用 'Uptime' 命令來查看 Linux 系統或伺服器已運行多久。uptime 命令會提供我們以下資訊:當前時間、系統已運行的時間、目前有多少用戶登入,以及過去 1、5 和 15 分鐘的系統負載平均值。今天,我偶然發現了一個類似的工具,名為 Tuptime,它是 uptime 命令的替代方案。Tuptime 是一個用 Python 編寫的命令行工具,可以報告 Linux 系統的歷史和統計實時運行時間,並在系統重啟間保持數據。它就像 Uptime,但顯示的輸出更有趣。在本指南中,我們將學習如何使用 Tuptime 工具來查找 Linux 系統的運行時間。

在進一步說明之前,讓我給你幾個使用「uptime」命令來查找系統運行時間的例子。

如果你在沒有任何參數的情況下執行 uptime 指令;

$ uptime

你將會看到類似下面的輸出。

09:53:21 up 18 min, 1 user, load average: 0.01, 0.03, 0.06

這裡

  • 09:53:21 - 當前時間,
  • 運行時間 18 分鐘 - 總運行時間,
  • 1 使用者 - 目前已登入的使用者數量,
  • 平均負載:0.01, 0.03, 0.06 - 過去 5、10 和 15 分鐘的系統平均負載。

要只顯示總運行時間,請執行:

$ uptime -p

或者,

$ uptime --pretty

要顯示系統啟動的日期和時間,請運行:

$ uptime --s

或者,

$ uptime --since

如你所見,Uptime 只有兩個選項(但已經足夠)。

與 Uptime 命令相比,Tuptime 工具列出了以下額外的詳細資訊:

  • 從您安裝 tuptime 工具的那一天起,系統的總運行時間,
  • 系統啟動和關機的總次數,
  • 正確或不正確完成的關閉總數,
  • 系統運行時間和停機時間百分比計數器,
  • 最大/最短/平均運行時間,
  • 最大/最短/平均停機時間,
  • 當前運行時間。

安裝 Tuptime

Tuptime 可在 AUR 使用,因此可以透過任何 AUR 輔助工具在 Arch Linux 及其衍生版本上安裝,如下所示。

$ yay -S tuptime

在 Debian、Ubuntu、Linux Mint 上:

$ sudo apt-get install tuptime

請確保您已在 Ubuntu 及其衍生版本上啟用 [Universe] 軟體庫,以安裝 tuptime。

可以使用以下指令啟用宇宙:

$ sudo add-apt-repository universe

在其他 Linux 發行版上,使用以下命令 git clone Tuptime 儲存庫:

$ git clone https://github.com/rfrail3/tuptime.git

將 "tuptime" 可執行檔案複製到您的 `PATH`,例如 “/usr/local/bin/”:

$ sudo cp tuptime/src/tuptime /usr/local/bin/tuptime

最後,將其設為可執行檔:

$ sudo chmod ugo+x /usr/local/bin/tuptime

使用 Tuptime 查找 Linux 系統運行時間詳情

要查看您的 Linux 系統的運行時間詳情,只需執行:

$ tuptime

範例輸出:

System startups: 1 since 07:24:35 AM 01/20/2020
System shutdowns: 0 ok - 0 bad
System uptime: 100.0 % - 15 minutes and 49 seconds
System downtime: 0.0 % - 0 seconds
System life: 15 minutes and 49 seconds

Largest uptime: 15 minutes and 49 seconds from 07:24:35 AM 01/20/2020
Shortest uptime: 15 minutes and 49 seconds from 07:24:35 AM 01/20/2020
Average uptime: 15 minutes and 49 seconds

Largest downtime: 0 seconds
Shortest downtime: 0 seconds
Average downtime: 0 seconds

Current uptime: 15 minutes and 49 seconds since 07:24:35 AM 01/20/2020

與傳統的 Uptime 命令不同,Tuptime 會從安裝當天起追蹤系統的重啟與關機情況。今天,我在我的 Ubuntu 伺服器上安裝了它,並重啟了系統兩次。Tuptime 追蹤了所有的系統啟動/關機,並以易於理解的方式呈現這些資訊。

看看以下輸出。

$ tuptime 
System startups: 3 since 07:24:35 AM 01/20/2020
System shutdowns: 2 ok <- 0 bad
System uptime: 43.18 % - 1 hour, 0 minutes and 27 seconds
System downtime: 56.82 % - 1 hour, 19 minutes and 32 seconds
System life: 2 hours, 19 minutes and 59 seconds

Largest uptime: 35 minutes and 1 second from 07:41:00 AM 01/20/2020
Shortest uptime: 9 minutes and 20 seconds from 09:35:14 AM 01/20/2020
Average uptime: 20 minutes and 9 seconds

Largest downtime: 1 hour, 19 minutes and 13 seconds from 08:16:01 AM 01/20/2020
Shortest downtime: 19 seconds from 07:40:41 AM 01/20/2020
Average downtime: 39 minutes and 46 seconds

Current uptime: 9 minutes and 20 seconds since 09:35:14 AM 01/20/2020

看到了嗎?系統啟動和關機次數、總運行/停機時間、平均運行/停機時間的詳細信息已相應更新。預設的 uptime 命令不會追蹤這些詳細資訊。

正常運作時間選項

Tuptime 提供了一些有用的選項,讓你可以按自己的喜好格式化輸出。

以表格列形式顯示輸出

要以表格列格式顯示系統運行時間,請使用-t參數。

$ tuptime -t
No.             Startup Date                               Uptime            Shutdown Date   End                            Downtime
                                                                                                                                    
1     07:24:35 AM 01/20/2020             16 minutes and 6 seconds   07:40:41 AM 01/20/2020    OK                          19 seconds
2     07:41:00 AM 01/20/2020              35 minutes and 1 second   08:16:01 AM 01/20/2020    OK   1 hour, 19 minutes and 13 seconds
3     09:35:14 AM 01/20/2020   2 hours, 57 minutes and 58 seconds
列表檢視

同樣地,我們可以使用 -l 參數將輸出顯示為 列表視圖。

$ tuptime -l
Startup:  1  at  07:24:35 AM 01/20/2020
Uptime:   16 minutes and 6 seconds
Shutdown: OK  at  07:40:41 AM 01/20/2020
Downtime: 19 seconds

Startup:  2  at  07:41:00 AM 01/20/2020
Uptime:   35 minutes and 1 second
Shutdown: OK  at  08:16:01 AM 01/20/2020
Downtime: 1 hour, 19 minutes and 13 seconds

Startup:  3  at  09:35:14 AM 01/20/2020
Uptime:   3 hours, 0 minutes and 42 seconds
新增核心詳細資料

我們甚至可以使用 -k 旗標將 核心資訊 加入輸出。

$ tuptime -k
System startups:	3   since   07:24:35 AM 01/20/2020
System shutdowns:	2 ok   <-   0 bad
System uptime: 		74.54 %   -   3 hours, 52 minutes and 54 seconds
System downtime: 	25.46 %   -   1 hour, 19 minutes and 32 seconds
System life: 		5 hours, 12 minutes and 26 seconds
System kernels: 	1

Largest uptime: 	3 hours, 1 minute and 47 seconds   from   09:35:14 AM 01/20/2020
...with kernel: 	Linux-4.15.0-74-generic-x86_64-with-Ubuntu-18.04-bionic
Shortest uptime:	16 minutes and 6 seconds   from   07:24:35 AM 01/20/2020
...with kernel: 	Linux-4.15.0-74-generic-x86_64-with-Ubuntu-18.04-bionic
Average uptime: 	1 hour, 17 minutes and 38 seconds

Largest downtime:	1 hour, 19 minutes and 13 seconds   from   08:16:01 AM 01/20/2020
...with kernel: 	Linux-4.15.0-74-generic-x86_64-with-Ubuntu-18.04-bionic
Shortest downtime:	19 seconds   from   07:40:41 AM 01/20/2020
...with kernel: 	Linux-4.15.0-74-generic-x86_64-with-Ubuntu-18.04-bionic
Average downtime: 	39 minutes and 46 seconds

Current uptime: 	3 hours, 1 minute and 47 seconds   since   09:35:14 AM 01/20/2020
...with kernel: 	Linux-4.15.0-74-generic-x86_64-with-Ubuntu-18.04-bionic
更改日期格式

也可以更改預設的人類可讀日期格式,並以秒為單位列印時間以及以紀元列印日期。

要做到這一點,請使用 -s 標誌。

$ tuptime -s
System startups:	3   since   1579505075
System shutdowns:	2 ok   <-   0 bad
System uptime: 		74.77 %   -   14140.49
System downtime: 	25.23 %   -   4772.32
System life: 		18912.81

Largest uptime: 	11073.81   from   1579512914
Shortest uptime:	965.97   from   1579505075
Average uptime: 	4713.5

Largest downtime:	4753.29   from   1579508161
Shortest downtime:	19.03   from   1579506041
Average downtime: 	2386.16

Current uptime: 	11073.81   since   1579512914
更改日期格式

預設情況下,Tuptime 會根據您的系統語系列印詳細資訊。不過,您可以使用 -d 參數按照您的喜好 更改日期格式,如下所示。

$ tuptime -d '%H:%M:%S %m-%d-%Y'
System startups:	3   since   07:24:35 01-20-2020
System shutdowns:	2 ok   <-   0 bad
System uptime: 		74.89 %   -   3 hours, 57 minutes and 15 seconds
System downtime: 	25.11 %   -   1 hour, 19 minutes and 32 seconds
System life: 		5 hours, 16 minutes and 47 seconds

Largest uptime: 	3 hours, 6 minutes and 8 seconds   from   09:35:14 01-20-2020
Shortest uptime:	16 minutes and 6 seconds   from   07:24:35 01-20-2020
Average uptime: 	1 hour, 19 minutes and 5 seconds

Largest downtime:	1 hour, 19 minutes and 13 seconds   from   08:16:01 01-20-2020
Shortest downtime:	19 seconds   from   07:40:41 01-20-2020
Average downtime: 	39 minutes and 46 seconds
以 CSV 格式顯示輸出

如有需要,您也可以將輸出列印成 CSV 格式。

$ tuptime --csv
"System startups","3","since","07:24:35 AM 01/20/2020"
"System shutdowns","2","ok","<-","0","bad"
"System uptime","75.2 %","-","4 hours, 1 minute and 10 seconds"
"System downtime","24.8 %","-","1 hour, 19 minutes and 32 seconds"
"System life","5 hours, 20 minutes and 43 seconds"
"Largest uptime","3 hours, 10 minutes and 4 seconds","from","09:35:14 AM 01/20/2020"
"Shortest uptime","16 minutes and 6 seconds","from","07:24:35 AM 01/20/2020"
"Average uptime","1 hour, 20 minutes and 23 seconds"
"Largest downtime","1 hour, 19 minutes and 13 seconds","from","08:16:01 AM 01/20/2020"
"Shortest downtime","19 seconds","from","07:40:41 AM 01/20/2020"
"Average downtime","39 minutes and 46 seconds"
"Current uptime","3 hours, 10 minutes and 4 seconds","since","09:35:14 AM 01/20/2020"
顯示自一年前的輸出

如果你讓你的系統運行多年,此選項可以獲取自一年前起的 Linux 系統運行時間。

$ tuptime --tsince -31557600

更多細節可以在手冊頁中找到。

$ man uptime
$ man tuptime

建議閱讀:

  • 如何使用 Uptimed 監控 Linux 系統運行時間
  • 如何在 Linux 中快速監控多個主機
  • 如何在 Linux 中查詢一個進程已運行多久

資源:

  • Tuptime GitHub 儲存庫