Software & AppsOperating SystemLinux

How To Display Time Elapsed Since Last System Boot using Uptime?

Ubuntu 20

In this article, we will explore how to display the time elapsed since the last system boot using the uptime command. This can be useful for system administrators who need to monitor system performance, troubleshoot issues, or simply want to know how long the system has been running without a reboot.

Quick Answer

To display the time elapsed since the last system boot using the uptime command, you can use the -s or --since option with the uptime command. This will show you the date and time of the last boot.

Understanding the Uptime Command

The uptime command in Linux gives you 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.

The basic syntax of the uptime command is as follows:

uptime [options]

The uptime command without any options will display the current time, the uptime, the number of users, and the load average.

Displaying the Time of the Last System Boot

To display the time of the last system boot, you can use the -s or --since option with the uptime command:

uptime -s

This command will display the date and time when the system was last booted.

Calculating the Time Elapsed Since the Last Boot

If you want to calculate the number of hours that have passed since the last boot, you can use the awk command in combination with uptime -s:

uptime -s | awk '{uptime=strftime("%H:%M:%S",systime()-mktime(substr($0,1,4)" "substr($0,6,2)" "substr($0,9,2)" "substr($0,12,2)" "substr($0,15,2)" "substr($0,18,2))); print uptime}'

This command uses awk to calculate the time difference between the current system time and the time of the last boot. The result is then formatted in hh:mm:ss format and printed.

Using the /proc/uptime Pseudo-file

Another way to get the time elapsed since the last boot is to use the /proc/uptime pseudo-file. This file contains two numbers: the first is the total number of seconds the system has been up, and the second is the total number of seconds spent in idle mode.

Here are a few ways to use /proc/uptime to get the time elapsed since the last boot:

  1. To get the time elapsed in hh:mm:ss format:
awk '{print int($1/3600)":"int(($1%3600)/60)":"int($1%60)}' /proc/uptime
  1. To get the time elapsed in seconds:
awk '{print $1}' /proc/uptime
  1. To get the time elapsed in minutes:
echo $(awk '{print $1}' /proc/uptime) / 60 | bc
  1. To get the time elapsed in hours:
echo $(awk '{print $1}' /proc/uptime) / 3600 | bc
  1. To get the time elapsed with a specific number of digits of precision (e.g., 2):
echo "scale=2; $(awk '{print $1}' /proc/uptime) / 3600" | bc
  1. To get the time elapsed in days, hours, minutes, and seconds:
awk '{print int($1/86400)"days "int($1%86400/3600)":"int(($1%3600)/60)":"int($1%60)}' /proc/uptime
  1. To get the time elapsed with zero padding:
awk '{printf("%02d:%02d:%02d",int($1/3600),int($1/3600/60),int($1%60))}' /proc/uptime

Note: Some of these commands require the bc command to be installed on your system.

Conclusion

In this article, we learned how to display the time elapsed since the last system boot using the uptime command and the /proc/uptime pseudo-file. We also learned how to use awk to manipulate the output of these commands to get the time elapsed in various formats. These commands are a valuable tool for any system administrator’s toolkit.

Remember, understanding your system’s uptime can help you make informed decisions about system maintenance, troubleshooting, and performance monitoring. So, make sure to use these commands to their full potential.

How can I check the uptime of my system using the `uptime` command?

To check the uptime of your system using the uptime command, simply open a terminal and type uptime. The command will display the current time, how long the system has been running, the number of users, and the load average.

How can I display the time of the last system boot using the `uptime` command?

To display the time of the last system boot using the uptime command, use the -s or --since option. Open a terminal and type uptime -s. The command will show you the date and time when the system was last booted.

How can I calculate the time elapsed since the last boot using the `uptime` command and `awk`?

To calculate the time elapsed since the last boot using the uptime command and awk, open a terminal and type uptime -s | awk '{uptime=strftime("%H:%M:%S",systime()-mktime(substr($0,1,4)" "substr($0,6,2)" "substr($0,9,2)" "substr($0,12,2)" "substr($0,15,2)" "substr($0,18,2))); print uptime}'. This command will give you the time elapsed in hh:mm:ss format.

How can I use the `/proc/uptime` pseudo-file to get the time elapsed since the last boot?

To use the /proc/uptime pseudo-file to get the time elapsed since the last boot, you can use various commands. For example, to get the time elapsed in hh:mm:ss format, type awk '{print int($1/3600)":"int(($1%3600)/60)":"int($1%60)}' /proc/uptime in a terminal. There are other commands mentioned in the article to get the time elapsed in different formats.

Do I need to have the `bc` command installed to use some of the commands mentioned?

Yes, some of the commands mentioned in the article require the bc command to be installed on your system. The bc command is a calculator that allows you to perform calculations in the terminal. You can install it using your package manager if it is not already installed.

Why is knowing the uptime of my system important?

Knowing the uptime of your system can be important for system administrators as it helps in monitoring system performance, troubleshooting issues, and determining the need for system maintenance or reboot. It provides valuable information about how long the system has been running without interruption.

Leave a Comment

Your email address will not be published. Required fields are marked *