如何顯示Linux系統的歷史和統計執行時間


系統管理員的重要工作之一是監控並檢查Linux系統運行了多長時間。持續跟蹤Linux系統的執行時間對於最佳化系統資源是一個好主意。本文介紹了**Tuptime工具**,它可以幫助系統管理員瞭解Linux機器運行了多長時間。

Tuptime工具用於報告Linux系統的歷史和統計執行時間(**uptime**),它在系統重啟之間跟蹤執行時間。

功能

  • 註冊首次啟動時間
  • 跟蹤系統啟動
  • 統計系統關機次數
  • 計算執行時間和停機時間
  • 計算最大、平均、最小執行時間和停機時間
  • 列印當前執行時間

安裝Tuptime工具

要安裝Tuptime工具,需要滿足以下預安裝要求:

  • 已安裝Python 2.7或3.X,但建議使用最新版本。
  • Python 模組 (sys, os, optparse, sqlite3, datetime, locale, platform, subprocess, time)。
  • Linux作業系統

要在Linux上查詢已安裝的python,請使用以下命令:

$ apt-cache search python | egrep "^python2.[0-9] " --color

示例輸出應如下所示:

python2.7 - Interactive high-level object-oriented language (version 2.7)

如果您的系統沒有安裝python,請使用以下命令安裝python:

$ sudo apt-get install python2.7

示例輸出應如下所示:

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
python3.1-minimal
Suggested packages:
python3.1-doc python3.1-profiler
The following NEW packages will be installed:
python3.1 python3.1-minimal
0 upgraded, 2 newly installed, 0 to remove and 13 not upgraded.
Need to get 5,444 kB of archives.
After this operation, 19.9 MB of additional disk space will be used.
Do you want to continue [Y/n]? y
Get:1 http://debian.osuosl.org/debian/ squeeze/main python3.1-minimal amd64 3.1.3-1 [1,669 kB]
Get:2 http://debian.osuosl.org/debian/ squeeze/main python3.1 amd64 3.1.3-1 [3,775 kB]
Fetched 5,444 kB in 27s (201 kB/s)
...........................

要從**git**克隆**Tuptime**儲存庫,請使用以下命令:

$ cd /tmp
$ git clone https://github.com/rfrail3/tuptime.git
$ ls
$ cd tuptime
$ ls

示例輸出應如下所示:

~$ cd /tmp
/tmp$ git clone https://github.com/rfrail3/tuptime.git
Cloning into 'tuptime'...
remote: Counting objects: 2171, done.
remote: Compressing objects: 100% (34/34), done.
remote: Total 2171 (delta 21), reused 0 (delta 0), pack-reused 2137
Receiving objects: 100% (2171/2171), 749.33 KiB | 296.00 KiB/s, done.
Resolving deltas: 100% (1056/1056), done.
Checking connectivity... done.
/tmp$ ls
aptdaemon-A0RrYd sni-qt_vlc_15046-UFPBar unity_support_test.0
config-err-X4Efq6 sni-qt_vlc_15117-3D0jOa
orbit-linux tuptime
/tmp$ cd tuptime
/tmp/tuptime$ ls
CHANGELOG LICENSE scripts tuptime-install.sh
debian README.md src tuptime-manual.txt

安裝**tuptime-install.sh**,如下所示:

# chmod +x tuptime-install.sh
# ./tuptime-install.sh

示例輸出應如下所示:

Tuptime installation script
Clonning repository...
Cloning into '/tmp/tmp.P6j2npOJVU'...
remote: Counting objects: 2171, done.
remote: Compressing objects: 100% (34/34), done.
remote: Total 2171 (delta 21), reused 0 (delta 0), pack-reused 2137
Receiving objects: 100% (2171/2171), 749.33 KiB | 318.00 KiB/s, done.
Resolving deltas: 100% (1056/1056), done.
Checking connectivity... done.
Copying files...
Creating tuptime user...
Creating tuptime db
Setting tuptime db ownership
Executing tuptime with tuptime user for testing
Copying init debian file...
update-rc.d: warning: default start runlevel arguments (2 3 4 5) do not match tuptime Default-Start values (1 2 3 4 5)
update-rc.d: warning: default stop runlevel arguments (0 1 6) do not match tuptime Default-Stop values (0 6)
....................................

使用Tuptime

要執行**Tuptime**,請使用以下命令:

# tuptime

示例輸出應如下所示:

System startups:    1 since 12:16:47 Tuesday 08 March 2016
System shutdowns:   0 ok - 0 bad
System uptime:      100.0 % - 1 day, 2 hours, 18 minutes and 8 seconds
System downtime:    0.0 % - 0 seconds
System life:        1 day, 2 hours, 18 minutes and 8 seconds
Largest uptime:     1 day, 2 hours, 18 minutes and 8 seconds from 12:16:47 Tuesday 08 March 2016
Shortest uptime:    1 day, 2 hours, 18 minutes and 8 seconds from 12:16:47 Tuesday 08 March 2016
Average uptime:     1 day, 2 hours, 18 minutes and 8 seconds
Largest downtime:   0 seconds
Shortest downtime:  0 seconds
Average downtime: 0 seconds
Current uptime:     1 day, 2 hours, 18 minutes and 8 seconds since 12:16:47 Tuesday 08 March 2016

要顯示包含日期和時間的輸出,請使用以下命令:

# tuptime --date='%H:%M:%S %d-%m-%Y'

示例輸出應如下所示:

System startups:    1 since 12:16:47 08-03-2016
System shutdowns:   0 ok - 0 bad
System uptime:      100.0 % - 1 day, 2 hours, 19 minutes and 41 seconds
System downtime:    0.0 % - 0 seconds
System life:        1 day, 2 hours, 19 minutes and 41 seconds
Largest uptime:     1 day, 2 hours, 19 minutes and 41 seconds from 12:16:47 08-03-2016
Shortest uptime:    1 day, 2 hours, 19 minutes and 41 seconds from 12:16:47 08-03-2016
Average uptime:     1 day, 2 hours, 19 minutes and 41 seconds
Largest downtime:   0 seconds
Shortest downtime:  0 seconds
Average downtime: 0 seconds
Current uptime:     1 day, 2 hours, 19 minutes and 41 seconds since 12:16:47 08-03-2016

要將系統檔案列印為列表,請使用以下命令:

# tuptime --list

示例輸出應如下所示:

Startup: 1 at 12:16:47 Tuesday 08 March 2016
Uptime: 1 day, 2 hours, 21 minutes and 8 seconds

要建立一個替代資料庫檔案來儲存**Tuptime**日誌,如下所示:

# tuptime --filedb /tmp/tuptime_testdb.db

示例輸出應如下所示:

System startups:    1 since 12:16:47 Tuesday 08 March 2016
System shutdowns:   0 ok - 0 bad
System uptime:      100.0 % - 1 day, 2 hours, 22 minutes and 36 seconds
System downtime:    0.0 % - 0 seconds
System life:        1 day, 2 hours, 22 minutes and 36 seconds
Largest uptime:     1 day, 2 hours, 22 minutes and 36 seconds from 12:16:47 Tuesday 08 March 2016
Shortest uptime:    1 day, 2 hours, 22 minutes and 36 seconds from 12:16:47 Tuesday 08 March 2016
Average uptime:     1 day, 2 hours, 22 minutes and 36 seconds
Largest downtime:   0 seconds
Shortest downtime:  0 seconds
Average downtime: 0 seconds
Current uptime:     1 day, 2 hours, 22 minutes and 36 seconds since 12:16:47 Tuesday 08 March 2016

要獲取關於最終狀態關機的資訊,請使用以下命令:

# tuptime --end --table

示例輸出應如下所示:

No. Startup Date Uptime Shutdown Date End Downtime

1 12:16:47 Tuesday 08 March 2016 1 day, 2 hours, 23 minutes and 42 seconds

要列印**核心**資訊,請使用以下命令:

# tuptime --kernel

示例輸出應如下所示:

System startups:    1 since 12:16:47 Tuesday 08 March 2016
System shutdowns:   0 ok - 0 bad
System uptime:     100.0 % - 1 day, 2 hours, 25 minutes and 4 seconds
System downtime:   0.0 % - 0 seconds
System life:       1 day, 2 hours, 25 minutes and 4 seconds
System kernels:    1
Largest uptime:    1 day, 2 hours, 25 minutes and 4 seconds from 12:16:47 Tuesday 08 March 2016
...with kernel:    Linux-4.2.0-30-generic-x86_64-with-Ubuntu-14.04-trusty
Shortest uptime:   1 day, 2 hours, 25 minutes and 4 seconds from 12:16:47 Tuesday 08 March 2016
...with kernel:    Linux-4.2.0-30-generic-x86_64-with-Ubuntu-14.04-trusty
Average uptime:    1 day, 2 hours, 25 minutes and 4 seconds
Largest downtime:  0 seconds
...with kernel:    None
Shortest downtime: 0 seconds
...with kernel:    None
Average downtime: 0 seconds
Current uptime:    1 day, 2 hours, 25 minutes and 4 seconds since 12:16:47 Tuesday 08 March 2016
...with kernel:    Linux-4.2.0-30-generic-x86_64-with-Ubuntu-14.04-trusty

要獲取**Tuptime**幫助資訊,請使用以下命令:

# tuptime --help

標準輸出應如下所示:

Usage: tuptime [options]
Options:
   -h, --help show this help message and exit
   -c, --ckernel classify / order by kernel
   -d DATE_FORMAT, --date=DATE_FORMAT date format output
   -e, --end order by end state
   -f FILE, --filedb=FILE database file
   -g, --graceful register a gracefully shutdown
   -k, --kernel print kernel information
   -l, --list enumerate system life as list
   -o, --offtime order by offtime / downtime
   -r, --reverse reverse order
   -s, --seconds output time in seconds and epoch
   -t, --table enumerate system life as table
   -u, --uptime order by uptime
   -v, --verbose verbose output
   -V, --version show version
   -x, --silent update values into db without output

恭喜!現在您知道“如何顯示Linux系統的歷史和統計執行時間”了。我們將在下一篇文章中學習更多關於這類Linux命令的內容。繼續關注!

更新於:2019年10月21日

瀏覽量:112

開啟您的職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.