通过示例为初学者解释 Linux uptime 命令
在此页
- Linux 正常运行时间命令
- Q1。如何使用正常运行时间命令
- Q2。如何使工具以漂亮的格式显示时间
- Q3。自系统启动以来,如何使正常运行时间显示日期/时间
- Q4。如何获取版本信息和一般帮助
- 结论
如果你是 Linux 新手,并且对系统管理感兴趣,或者你想成为高级用户,那么你需要对命令行有扎实的了解。您应该了解几个命令,其中之一是正常运行时间。在本文中,我们将使用一些易于理解的示例来讨论此命令的基础知识。
但在此之前,值得一提的是,本教程中使用的所有示例都已在 Ubuntu 18.04 LTS 机器上进行了测试。
Linux 运行时间命令
顾名思义,uptime 命令为您提供系统已启动(或运行)的时间。这是它的语法:
uptime [options]
这是工具手册页解释它的方式:
uptime gives a one line display of the following information. The current time, how long the system
has been running, how many users are currently logged on, and the system load averages for the past
1, 5, and 15 minutes.
以下问答式示例应该可以让您更好地了解 uptime 命令的工作原理。
Q1。如何使用正常运行时间命令
Uptimes 的基本用法非常简单 - 只需输入命令名称并按回车键即可。
uptime
这是该工具产生的输出类型:

所以第一个条目是当前时间,然后 up 表示系统正在运行,5:53 是系统已启动的总时间,最后是系统平均负载。以防万一您想了解更多信息,以下是 uptime 手册页关于最后一个条目的内容:
System load averages is the average number of processes that are either in a runnable or
uninterruptable state. A process in a runnable state is either using the CPU or waiting to use the
CPU. A process in uninterruptable state is waiting for some I/O access, eg waiting for disk.
The averages are taken over the three time intervals. Load averages are not normalized for the
number of CPUs in a system, so a load average of 1 means a single CPU system is loaded all the
time while on a 4 CPU system it means it was idle 75% of the time.
Q2。如何使工具以漂亮的格式显示时间
如果您只想知道系统启动的时间,并且以更易于阅读的格式显示,请使用 -p 命令行选项。
uptime -p
这是在我们的案例中生成的此命令的输出:

Q3.自系统启动以来,如何使正常运行时间显示日期/时间
您还可以使 uptime 专门显示自系统运行以来的时间/日期。这可以使用 -s 命令行选项来完成。
uptime -s
这是我们案例中生成的命令的输出:

Q4.如何获取版本信息和一般帮助
使用 -V 选项获取版本信息,使用 -h 获取一般帮助。
uptime -V
uptime -h

结论
正如您所观察到的,uptime 命令很容易理解和使用。它不提供很多功能(或命令行选项)。这里讨论了它提供的所有内容。因此,只需练习这些选项,您就应该准备好在日常工作中使用正常运行时间。以备不时之需,这里是工具手册页。