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.

2 comments:

Anonymous said...

Wow, very simple and elegant. Thank you!

Pablo AB said...

I just use "uptime" command...