Pages

script: uptime.sh: get uptime from /proc/uptime

Just a simple script to get uptime from /proc/uptime (which is in seconds)

#!/bin/bash
# uptime.sh
# get uptime from /proc/uptime

uptime=$(</proc/uptime)
uptime=${uptime%%.*}

seconds=$(( uptime%60 ))
minutes=$(( uptime/60%60 ))
hours=$(( uptime/60/60%24 ))
days=$(( uptime/60/60/24 ))

echo "$days days, $hours hours, $minutes minutes, $seconds seconds"

Ofcourse in the same way you could add months, years, decades etc.
you got the idea.

6 comments:

Anonymous said...

Wow, very simple and elegant. Thank you!

Pablo AB said...

I just use "uptime" command...

Anonymous said...

2014 and still helping!
Thanks for this because /usr/bin/uptime was a pain for scripting.

Anonymous said...

2014 and still helps!
Thank you for this because /usr/bin/uptime was a pain for scripting.

Anonymous said...

2014 and still helps!
Thank you for this because /usr/bin/uptime was a pain for scripting.

Anonymous said...

i apologize for the quadruple post. The comment page reloading itself, lead me to believe the comment was not accepted.