Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I want to display time elapsed since last system boot using "uptime" command, but I don't want it to display all that info, I just want to display how many hours passed since last system boot (i.e. : 18:17:59 )

Thanks

share|improve this question
add comment

3 Answers

up vote 3 down vote accepted

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

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

or, simple:

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.

share|improve this answer
add comment

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
share|improve this answer
add comment

Try this one:

uptime | awk '{ print $3 }'

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

share|improve this answer
2  
It works. But it doesn't seems the right way to do it. What if the third word isn't the time, for example my third word is "4" , because my machine is running for 4 days. This is what I get: "02:00:39 up 4 days, 12:54, 2 users, load average: 0.03. 0.06, 0.06" . –  Adrian George Aug 21 '13 at 23:03
    
Oh :D I didn’t know about the time more than 1 day! –  AliNa Aug 21 '13 at 23:45
    
It ok :d now you have some answers to this? –  Adrian George Aug 21 '13 at 23:47
    
No, sorry. uptime seems to have no options but I couldn’t find anything on the net either. –  AliNa Aug 22 '13 at 0:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.