1. Overview
One of the basic health checks for a system is how long it has been running. This check provides us with an idea about the exact last boot time of the system. Based on that, we can make decisions in terms of a given machine’s usage.
In some cases, the recently started system tells us that it is freshly configured. However, we may encounter a case where the system was configured a long time ago but recently rebooted.
In this tutorial, we’ll see different commands to find how long a system has been running.
2. The uptime Command
The uptime command is popular and prominently used in Linux communities. It displays a bunch of system-related information. Specifically, after the first column, the output contains the length of time the system has been running. Let’s take a look at an example:
$ uptime
08:08:53 up 176 days, 21:27, 2 users, load average: 0.00, 0.02, 0.00
Now, we can understand the meaning of each of the output fields from this command:
- 08:08:53 – the current time in UTC format
- up – the state of the system (as opposed to down)
- 176 days, 21:27 – number of days, hours, and minutes the system has been running
- 2 users – users currently logged on to the system
- load average: 0.00, 0.02, 0.00 – load average of the system for the past one, five, and 15 minutes, respectively
We may want to get only the required information from this output*.* To do so, we can use various options:
$ uptime -p
up 25 weeks, 1 day, 21 hours, 58 minutes
With -p, uptime shows only the system’s running time in a human-readable format. The format of the system running time is in weeks, days, hours, and minutes.
Although this may be pretty, it doesn’t show the exact date and time when the system was last started. Let’s look at another useful option that solves this problem:
$ uptime -s
2022-04-12 10:41:21
The -s flag stands for “since” and tells uptime to display the exact date and time the system was last booted.
3. Other Commands
In this section, we’re going to look at some other Linux commands that we can use to get the system running time information.
3.1. Using last reboot Command
The command last with its reboot subcommand displays all previous reboots of the system:
$ last reboot
reboot system boot 2.6.32-100.27.5. Thu Sep 14 20:38 - 23:55 (137+06:16)
reboot system boot 2.6.32-100.27.5. Sun Aug 24 21:28 - 23:37 (15+09:08)
wtmp begins Thu Sun 24 17:28:47 2022
The output shown above is an excerpt from the /var/log/wtmp file in the system. Because of this, last reboot can return no output if the records have been cleared. Hence, this might not be an effective solution.
3.2. Using who -b Command
Usually, the who command outputs data about the users currently logged into the system. However, there are many options for different purposes:
$ who -b
system boot 2022-04-12 10:41
In particular, the -b switch provides the precise date and time when the machine booted for the last time.
3.3. Using top
We often use the command top for getting information about the running processes on a Linux machine. In particular, it shows CPU, Memory, and other resource consumption. As an illustration, let’s see a snippet of the top command’s result:
$ top
top - 10:01:59 up 634 days, 18 min, 2 users, load average: 0.06, 0.04, 0.00
Tasks: 158 total, 1 running, 157 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.2 us, 0.3 sy, 0.0 ni, 99.5 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 7814720 total, 746064 free, 2063180 used, 5005476 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 5310780 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
15581 ferra 20 0 16.000t 1.008g 54588 S 3.0 13.5 339:07.78 ferra
4248 root 20 0 1462772 24872 14608 S 0.3 0.3 2:23.98 sym-agent-worke
...
In effect, the result includes a good amount of information like the number of processes, their categories, cpu and memory usage, and others.
Among all of these data, the top command output also shows the system uptime at the top left corner of the screen with a format similar to the uptime command: 10:01:59 up 634 days, 18 min.
4. Conclusion
In this article, we learned various Linux commands to check the duration for which a system has been running. These commands are also useful when we want to understand the health and history of a Linux machine.