How to display time elapsed since last system boot using "uptime"?

Solution 1:

To get the time elapsed since last system boot in hh:mm:ss format, you can use:

awk '{print int($1/3600)":"int(($1%3600)/60)":"int($1%60)}' /proc/uptime

/proc/uptime pseudo-file contains two numbers:

  • The first number is how long the system has been up in seconds.
  • The second number is how much of that time the machine has spent idle in seconds.

So, using awk you can take firs number and convert it in hh:mm:ss format.

Solution 2:

To get uptime in seconds:

awk '{print $1}' /proc/uptime

To get uptime in minutes:

 echo $(awk '{print $1}' /proc/uptime) / 60 | bc

To get uptime in hours:

 echo $(awk '{print $1}' /proc/uptime) / 3600 | bc

To get x digits of precision you can add scale=x, e.g. for x=2

echo "scale=2; $(awk '{print $1}' /proc/uptime) / 3600" | bc

Solution 3:

Try this one:

uptime | awk '{ print $3 }'

In fact, it prints the third word of the line produced by uptime.

Solution 4:

A trivial modification to show days:

awk '{print int($1/86400)"days "int($1%86400/3600)":"int(($1%3600)/60)":"int($1%60)}' /proc/uptime