Tuptime-查找Linux系统正常运行时间的CLI实用程序

时间:2020-03-21 11:48:28  来源:igfitidea点击:

通常,我们使用“正常运行时间”命令来查找Linux系统或者服务器已运行了多长时间。
uptime命令将为我们提供详细信息,例如当前时间,系统运行了多长时间,当前登录了多少用户以及过去1、5和15分钟的平均系统负载。
今天,我偶然发现了一个名为Tuptime的类似实用程序,它是uptime命令的替代品。
Tuptime是一个用Python编写的命令行实用程序,用于报告Linux系统的历史和统计实时时间,并在两次重启之间保持同步。
就像正常运行时间一样,但是显示出更多有趣的输出。
在本教程中,我们将了解如何使用Tuptime实用程序查找Linux系统正常运行时间。

在继续之前,让我为我们提供一些使用“ 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

如我们所见,正常运行时间只有两个选项(尽管足够了)。

与正常运行时间命令相比,Tuptime实用程序列出了以下其他详细信息:

  • 从安装tuptime实用工具之日算起的总系统正常运行时间,
  • 系统启动和关闭的总数,
  • 正确或者不正确完成的关机总数,
  • 系统正常运行时间和停机时间计数器,以百分比表示,
  • 最大/最短/平均正常运行时间,
  • 最大/最短/平均停机时间,
  • 当前的正常运行时间。

安装Tuptime

Tuptime在AUR中可用,因此可以使用如下所示的任何AUR帮助器将其安装在Arch Linux及其变体中。

$yay -S tuptime

在Debian,Ubuntu和Linux Mint上:

$sudo apt-get install tuptime

确保已在Ubuntu及其衍生版本上启用[Universe]存储库以安装tuptime。

可以使用以下命令启用Universe:

$sudo add-apt-repository universe

在其他Linux发行版上,git使用以下命令克隆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/2017
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/2017
Shortest uptime: 15 minutes and 49 seconds from 07:24:35 AM 01/20/2017
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/2017

与传统的Uptime命令不同,Tuptime将跟踪从安装之日起系统的重新引导和关闭。
今天,我将其安装在Ubuntu服务器中,然后重新启动了系统两次。
Tuptime跟踪所有系统启动/关闭,并以一种易于理解的方式显示该信息。

看一下下面的输出。

$tuptime 
System startups: 3 since 07:24:35 AM 01/20/2017
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/2017
Shortest uptime: 9 minutes and 20 seconds from 09:35:14 AM 01/20/2017
Average uptime: 20 minutes and 9 seconds
Largest downtime: 1 hour, 19 minutes and 13 seconds from 08:16:01 AM 01/20/2017
Shortest downtime: 19 seconds from 07:40:41 AM 01/20/2017
Average downtime: 39 minutes and 46 seconds
Current uptime: 9 minutes and 20 seconds since 09:35:14 AM 01/20/2017

看到了吗?
系统启动和关闭计数,总正常运行时间/停机时间,平均正常运行时间/停机时间详细信息也会相应更新。
默认的正常运行时间命令将不会跟踪此类详细信息。

Tuptime选项

Tuptime附带了一些有用的选项,可根据需要格式化输出。

在表格列视图中显示输出

要以表格列格式显示系统正常运行时间,请使用-t标志。

$tuptime -t
No.             Startup Date                               Uptime            Shutdown Date   End                            Downtime

1     07:24:35 AM 01/20/2017             16 minutes and 6 seconds   07:40:41 AM 01/20/2017    OK                          19 seconds
2     07:41:00 AM 01/20/2017              35 minutes and 1 second   08:16:01 AM 01/20/2017    OK   1 hour, 19 minutes and 13 seconds
3     09:35:14 AM 01/20/2017   2 hours, 57 minutes and 58 seconds

列表显示

同样,我们可以将输出显示为带有-l标志的列表视图。

$tuptime -l
Startup:  1  at  07:24:35 AM 01/20/2017
Uptime:   16 minutes and 6 seconds
Shutdown: OK  at  07:40:41 AM 01/20/2017
Downtime: 19 seconds
Startup:  2  at  07:41:00 AM 01/20/2017
Uptime:   35 minutes and 1 second
Shutdown: OK  at  08:16:01 AM 01/20/2017
Downtime: 1 hour, 19 minutes and 13 seconds
Startup:  3  at  09:35:14 AM 01/20/2017
Uptime:   3 hours, 0 minutes and 42 seconds

添加内核详细信息

我们甚至可以使用-k标志在输出中添加内核信息。

$tuptime -k
System startups:	3   since   07:24:35 AM 01/20/2017
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/2017
...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/2017
...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/2017
...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/2017
...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/2017
...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-2017
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-2017
Shortest uptime:	16 minutes and 6 seconds   from   07:24:35 01-20-2017
Average uptime: 	1 hour, 19 minutes and 5 seconds
Largest downtime:	1 hour, 19 minutes and 13 seconds   from   08:16:01 01-20-2017
Shortest downtime:	19 seconds   from   07:40:41 01-20-2017
Average downtime: 	39 minutes and 46 seconds

以CSV格式显示输出

如果需要,我们也可以以CSV格式打印输出。

$tuptime --csv
"System startups","3","since","07:24:35 AM 01/20/2017"
"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/2017"
"Shortest uptime","16 minutes and 6 seconds","from","07:24:35 AM 01/20/2017"
"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/2017"
"Shortest downtime","19 seconds","from","07:40:41 AM 01/20/2017"
"Average downtime","39 minutes and 46 seconds"
"Current uptime","3 hours, 10 minutes and 4 seconds","since","09:35:14 AM 01/20/2017"

从一年前开始显示输出

如果我们将系统闲置了多年,那么自一年前以来,此选项可以使Linux系统正常运行。

$tuptime --tsince -31557600

可以在手册页中找到更多详细信息。

$man uptime
$man tuptime