如何显示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

开启您的职业生涯

通过完成课程获得认证

开始学习
广告