如何获取正常运行时间历史记录?

如何获取正常运行时间历史记录?

我可以从中获取正常运行时间uptime,但是历史上启动和关机之间的时间又如何呢?

如果最后一部分是持续时间(天+小时:分钟),则重启之间的时间重叠是没有意义的

uptime
 04:32:34 up 7 days,  1:45,  6 users,  load average: 0.72, 1.80, 2.43
last -F | grep -i boot
reboot   system boot  6.0.12-76060006- Wed Feb 15 10:49:34 2023 - Thu Feb 16 23:37:21 2023 (1+12:47)
reboot   system boot  6.0.12-76060006- Tue Feb 14 18:25:20 2023 - Thu Feb 16 23:37:21 2023 (2+05:12)
reboot   system boot  6.0.12-76060006- Tue Feb 14 14:13:15 2023 - Tue Feb 14 18:24:37 2023  (04:11)
reboot   system boot  6.0.12-76060006- Sun Feb 12 18:04:08 2023 - Tue Feb 14 14:03:15 2023 (1+19:59)
reboot   system boot  6.0.12-76060006- Sun Feb 12 17:45:53 2023 - Tue Feb 14 14:03:15 2023 (1+20:17)
reboot   system boot  6.0.12-76060006- Fri Feb 10 21:43:35 2023 - Sun Feb 12 17:41:39 2023 (1+19:58)
reboot   system boot  6.0.12-76060006- Thu Feb  9 02:40:16 2023 - Fri Feb 10 21:42:55 2023 (1+19:02)
reboot   system boot  6.0.12-76060006- Thu Feb  9 01:15:53 2023 - Fri Feb 10 21:42:55 2023 (1+20:27)
reboot   system boot  6.0.12-76060006- Tue Feb  7 13:13:54 2023 - Fri Feb 10 21:42:55 2023 (3+08:29)
reboot   system boot  6.0.12-76060006- Tue Feb  7 13:11:14 2023 - Fri Feb 10 21:42:55 2023 (3+08:31)
reboot   system boot  6.0.12-76060006- Mon Feb  6 17:45:21 2023 - Fri Feb 10 21:42:55 2023 (4+03:57)
reboot   system boot  6.0.12-76060006- Sat Jan 28 23:29:20 2023 - Fri Feb 10 21:42:55 2023 (12+22:13)
reboot   system boot  6.0.12-76060006- Sat Jan 28 17:18:26 2023 - Fri Feb 10 21:42:55 2023 (13+04:24)
reboot   system boot  6.0.12-76060006- Sun Jan 22 17:03:11 2023 - Fri Feb 10 21:42:55 2023 (19+04:39)
reboot   system boot  6.0.12-76060006- Fri Jan 20 17:21:38 2023 - Sun Jan 22 17:02:27 2023 (1+23:40)
reboot   system boot  6.0.12-76060006- Fri Jan 20 17:17:16 2023 - Fri Jan 20 17:20:58 2023  (00:03)
reboot   system boot  6.0.12-76060006- Fri Jan 20 17:00:34 2023 - Fri Jan 20 17:20:58 2023  (00:20)
reboot   system boot  6.0.12-76060006- Thu Jan 19 22:21:51 2023 - Fri Jan 20 16:46:30 2023  (18:24)
reboot   system boot  6.0.12-76060006- Wed Jan 18 19:25:31 2023 - Thu Jan 19 22:21:08 2023 (1+02:55)
reboot   system boot  6.0.12-76060006- Tue Jan 17 21:59:16 2023 - Thu Jan 19 22:21:08 2023 (2+00:21)
reboot   system boot  6.0.12-76060006- Mon Jan 16 14:44:11 2023 - Tue Jan 17 21:58:23 2023 (1+07:14)
reboot   system boot  6.0.12-76060006- Fri Jan 13 16:01:13 2023 - Mon Jan 16 14:43:28 2023 (2+22:42)
reboot   system boot  6.0.12-76060006- Mon Jan  2 01:11:15 2023 - Fri Jan 13 16:00:26 2023 (11+14:49)

答案1

以下是一个小的 bash 代码片段,您可以使用它来显示 journalctl 报告的第一条和最后一条日志消息的秒数。它与 uptime 并不完全相同,但它确实给您留下了相当一致的印象。它只是将报告的日期解析为 UNIX 时间戳并显示差值,因此以秒为单位。

journalctl --list-boots | \
  awk 'NR > 1 {print $4"T"$5" "$8"T"$9}' | \
  while read t1 t2; do \
     echo $(( "$(date +"%s" -s "$t2")" - "$(date +"%s" -s "$t1")" )); 
  done;

相关内容