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

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.


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

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

Tags:

Command Line