有没有办法从BIOS或CPU读取Linux系统的总运行时间?
我已经通过 dmidecode 搜索了 BIOS 信息。但它给出的发布日期不适合我的问题。
然后我就退房了/proc
。但它保留了上次重新启动后的正常运行时间值。也许,为每次启动写入这些正常运行时间值可能是一种选择。
然后我查了一下dumpe2fs
。它给出了特定硬盘驱动器的总运行时间。这对我来说毫无用处,因为在我的应用程序运行时可以更改硬盘。
除了上述之外,我如何读取或计算系统的总运行时间?我可以从哪里阅读?
答案1
据我所知,这不是固件跟踪的内容。即使 BMC 也不测量总的正常运行时间。
这对记录之前启动的正常运行时间没有帮助,但您现在可以通过安装以下工具来开始记录正常运行时间uptimed
并对其进行设置,使其永远不会丢弃值(LOG_MAXIMUM_ENTRIES
在 中设置为 0 uptimed.conf
)。这将测量操作系统的正常运行时间,而不是总的 CPU“开启”时间,但它应该足够接近......一旦你开始uptimed
运行,你可以运行uprecords
来查看总数,例如
up 1492 days, 02:57:18 | since Sat Sep 7 00:50:06 2013
down 61 days, 08:11:24 | since Sat Sep 7 00:50:06 2013
%up 96.051 | since Sat Sep 7 00:50:06 2013
正如所指出的不切实际的,您将能够通过查看日志了解历史正常运行时间。如果您正在运行 systemd,则可以使用 来查看已记录的启动journalctl --list-boots
。不过,日志轮换意味着这可能会错过相当多的正常运行时间。
正如所指出的杰德BP,last reboot
可能会为您提供更长的靴子列表以及相关的正常运行时间。
答案2
如果您从操作系统获取该信息没有问题,则可以使用启动时间您可以查看Linux系统总时间的完整报告,包括系统崩溃。
例如,作为默认输出和恢复:
# tuptime
System startups: 8 since 08:32:29 AM 11/24/2016
System shutdowns: 3 ok - 4 bad
System uptime: 99.99 % - 1 year, 195 days, 5 hours, 47 minutes and 14 seconds
System downtime: 0.01 % - 1 hour, 6 minutes and 34 seconds
System life: 1 year, 195 days, 6 hours, 53 minutes and 48 seconds
Largest uptime: 240 days, 7 hours, 38 minutes and 10 seconds from 08:41:51 AM 02/07/2017
Shortest uptime: 18 hours, 15 minutes and 14 seconds from 02:26:05 PM 02/06/2017
Average uptime: 70 days, 0 hours, 43 minutes and 24 seconds
Largest downtime: 45 minutes and 15 seconds from 10:00:01 AM 03/14/2018
Shortest downtime: 5 seconds from 02:26:00 PM 02/06/2017
Average downtime: 9 minutes and 31 seconds
Current uptime: 85 days, 4 hours, 41 minutes and 1 second since 10:45:16 AM 03/14/2018
或者,可以使用 list 参数获取包含所有历史事件的列表,其中您可以了解关闭事件的情况:坏(崩溃)或正常(关闭过程之后):
# tuptime -l
Startup: 1 at 08:32:29 AM 11/24/2016
Uptime: 46 days, 16 hours, 52 minutes and 32 seconds
Shutdown: BAD at 01:25:01 AM 01/10/2017
Downtime: 5 minutes and 10 seconds
Startup: 2 at 01:30:11 AM 01/10/2017
Uptime: 27 days, 12 hours, 55 minutes and 49 seconds
Shutdown: OK at 02:26:00 PM 02/06/2017
Downtime: 5 seconds
Startup: 3 at 02:26:05 PM 02/06/2017
Uptime: 18 hours, 15 minutes and 14 seconds
Shutdown: OK at 08:41:19 AM 02/07/2017
Downtime: 32 seconds
Startup: 4 at 08:41:51 AM 02/07/2017
Uptime: 240 days, 7 hours, 38 minutes and 10 seconds
Shutdown: BAD at 05:20:01 PM 10/05/2017
Downtime: 3 minutes and 17 seconds
Startup: 5 at 05:23:18 PM 10/05/2017
Uptime: 7 days, 14 hours, 56 minutes and 43 seconds
Shutdown: BAD at 08:20:01 AM 10/13/2017
Downtime: 11 minutes and 35 seconds
Startup: 6 at 08:31:36 AM 10/13/2017
Uptime: 25 days, 1 hour, 7 minutes and 4 seconds
Shutdown: OK at 08:38:40 AM 11/07/2017
Downtime: 39 seconds
Startup: 7 at 08:39:19 AM 11/07/2017
Uptime: 127 days, 1 hour, 20 minutes and 42 seconds
Shutdown: BAD at 10:00:01 AM 03/14/2018
Downtime: 45 minutes and 15 seconds
Startup: 8 at 10:45:16 AM 03/14/2018
Uptime: 85 days, 4 hours, 42 minutes and 9 seconds
考虑到这一点last reboot
并journalctl --list-boots
从日志中获取信息,并且这些日志有最长寿命。相反,tuptime
将信息存储在专用于它的特定数据库文件中。
对于安装,假设您使用 Linux,该软件包可在 Debian 及其衍生版本中找到:
# apt-get install tuptime
如果没有,您可以从存储库获取安装脚本“tuptime-install.sh”: https://github.com/rfrail3/tuptime/