Linux

安装uptimed工具

目前主流的 Linux 发行版的官方存软件库中已经提供了 uptimed
在 Arch Linux 上安装:

  1. $ sudo pacman -S uptimed

在 Debian,Ubuntu,Linux Mint 上安装:

  1. $ sudo apt-get install uptimed

在 Ubuntu 及其衍生版本上安装前请确保添加了 universe 存储库,没有的话请运行以下命令添加:

  1. $ sudo add-apt-repository universe

在 Fedora 上安装:

  1. $ sudo dnf install uptimed

在 CentOS 7 上安装:
因为 CentOS 7 的默认存储库中没有 uptimed,所以要先添加 EPEL 存储库:

  1. $ sudo yum install epel-release

然后运行以下命令安装:

  1. $ sudo yum install uptimed

手动安装:
对于其他 Linux 系统,或者上述方法安装失败的,可以从发行版页面下载源码,然后手动编译安装:

  1. # wget https://github.com/rpodgorny/uptimed/archive/v0.4.2.zip -O uptimed.zip
  2. # unzip uptimed.zip
  3. # cd uptimed-0.4.2/
  4. # ./configure
  5. # make
  6. # make install

安装完成后,启用并启动服务:

  1. # systemctl enable uptimed
  2. # systemctl start uptimed

检查服务的启动状态:

  1. # systemctl status uptimed
  2. uptimed.service - Uptime record tracking daemon
  3. Loaded: loaded (/usr/lib/systemd/system/uptimed.service; disabled; vendor preset: disabled)
  4. Active: active (running) since Tue 2020-02-11 14:21:12 IST; 1s ago
  5. Docs: man:uptimed(8)
  6. man:uprecords(1)
  7. Main PID: 1435 (uptimed)
  8. Status: "Next milestone (five days) at Sun Feb 16 14:13:59 2020"
  9. CGroup: /system.slice/uptimed.service
  10. └─1435 /usr/sbin/uptimed -f
  11. Feb 11 14:21:12 server.ostechnix.local systemd[1]: Starting Uptime record tracking daemon...
  12. Feb 11 14:21:12 server.ostechnix.local systemd[1]: Started Uptime record tracking daemon.

image.png
这样,前期的安装工作就算完成了。

使用uptimed工具

安装完成 uptimed 工具之后,运行 uprecords 命令就能显示系统运行时间的历史摘要了:

  1. # uprecords

输出的摘要结果:

  1. # Uptime | System Boot up
  2. ----------------------------+---------------------------------------------------
  3. 1 0 days, 02:35:51 | Linux 3.10.0-1062.1.1.el Tue Feb 11 14:22:33 2020
  4. 2 0 days, 00:07:27 | Linux 3.10.0-1062.1.1.el Tue Feb 11 14:13:59 2020
  5. -> 3 0 days, 00:02:11 | Linux 3.10.0-1062.1.1.el Tue Feb 11 17:02:29 2020
  6. ----------------------------+---------------------------------------------------
  7. 1up in 0 days, 00:05:17 | at Tue Feb 11 17:09:56 2020
  8. no1 in 0 days, 02:33:41 | at Tue Feb 11 19:38:20 2020
  9. up 0 days, 02:45:29 | since Tue Feb 11 14:13:59 2020
  10. down 0 days, 00:05:12 | since Tue Feb 11 14:13:59 2020
  11. %up 96.953 | since Tue Feb 11 14:13:59 2020

image.png
这里的 -> 符号表示定位到了系统最近的一次启动时间。

使用 -b 按启动时间正序输出:

  1. # uprecords -b
  2. # Uptime | System Boot up
  3. ----------------------------+---------------------------------------------------
  4. 1 0 days, 00:07:27 | Linux 3.10.0-1062.1.1.el Tue Feb 11 14:13:59 2020
  5. 2 0 days, 02:35:51 | Linux 3.10.0-1062.1.1.el Tue Feb 11 14:22:33 2020
  6. -> 3 0 days, 00:30:14 | Linux 3.10.0-1062.1.1.el Tue Feb 11 17:02:29 2020

使用 -B 按启动时间倒序输出:

  1. # uprecords -B
  2. # Uptime | System Boot up
  3. ----------------------------+---------------------------------------------------
  4. -> 1 0 days, 00:14:51 | Linux 3.10.0-1062.1.1.el Tue Feb 11 17:02:29 2020
  5. 2 0 days, 02:35:51 | Linux 3.10.0-1062.1.1.el Tue Feb 11 14:22:33 2020
  6. 3 0 days, 00:07:27 | Linux 3.10.0-1062.1.1.el Tue Feb 11 14:13:59 2020

使用 -m 自定义输出记录数:

不加参数的话结果默认输出前 10 条记录,想要输出前 20 条记录,请运行:

  1. # uprecords -m 20

使用 -w 输出完整信息:

上面的结果中关于 Linux 的内核信息是被省略了部分内容的,想要输出完整的内核信息请用 -w 进行宽输出:

  1. # uprecords -w
  2. # Uptime | System Boot up
  3. ----------------------------+---------------------------------------------------
  4. 1 0 days, 02:35:51 | Linux 3.10.0-1062.1.1.el7.x86_64 Tue Feb 11 14:22:33 2020
  5. -> 2 0 days, 00:16:35 | Linux 3.10.0-1062.1.1.el7.x86_64 Tue Feb 11 17:02:29 2020
  6. 3 0 days, 00:07:27 | Linux 3.10.0-1062.1.1.el7.x86_64 Tue Feb 11 14:13:59 2020
  7. ----------------------------+---------------------------------------------------
  8. no1 in 0 days, 02:19:17 | at Tue Feb 11 19:38:21 2020
  9. up 0 days, 02:59:53 | since Tue Feb 11 14:13:59 2020
  10. down 0 days, 00:05:12 | since Tue Feb 11 14:13:59 2020
  11. %up 97.190 | since Tue Feb 11 14:13:59 2020

使用 -d 显示关机时间:

-d 可以将上面的内核信息替换为关机时间信息:

  1. # uprecords -d
  2. # Uptime | Last downtime Boot up
  3. ----------------------------+---------------------------------------------------
  4. 1 0 days, 02:35:51 | 0 days, 00:01:07 Tue Feb 11 14:22:33 2020
  5. -> 2 0 days, 00:13:02 | 0 days, 00:04:05 Tue Feb 11 17:02:29 2020
  6. 3 0 days, 00:07:27 | 0 days, 00:00:00 Tue Feb 11 14:13:59 2020
  7. ----------------------------+---------------------------------------------------
  8. no1 in 0 days, 02:22:50 | at Tue Feb 11 19:38:21 2020
  9. up 0 days, 02:56:20 | since Tue Feb 11 14:13:59 2020
  10. down 0 days, 00:05:12 | since Tue Feb 11 14:13:59 2020
  11. %up 97.136 | since Tue Feb 11 14:13:59 2020

查看更多的命令选项:

  1. # uprecords -?
  2. usage: uprecords [OPTION]...
  3. -? this help
  4. -a do not print ansi codes
  5. -b sort by boottime
  6. -B reverse sort by boottime
  7. -k sort by sysinfo
  8. -K reverse sort by sysinfo
  9. -d print downtime seen before every uptimes instead of system
  10. -c do not show current entry if not in top entries
  11. -f run continously in a loop
  12. -s do not print extra statistics
  13. -w wide output (more than 80 cols per line)
  14. -i INTERVAL use INTERVAL seconds for loop instead of 5, implies -f
  15. -m COUNT show a maximum of top COUNT entries instead of 10
  16. -M show next milestone
  17. -v version information

上面所举的例子都是日常使用中十分常见的,如果想要了解更多关于 uptimed 的用法,可以参考它的帮助手册。

  1. $ man uprecords