Merge pull request #380 from dylanaraps/uptime-fix

Rewrite uptime for Linux/Windows/Solaris to match macOS/BSD
This commit is contained in:
Dylan Araps 2016-10-21 23:45:25 +11:00 committed by GitHub
commit d9e15e3597

View file

@ -268,73 +268,52 @@ getkernel() {
# Uptime {{{ # Uptime {{{
getuptime() { getuptime() {
# Get uptime in seconds
case "$os" in case "$os" in
"Linux" | "Windows") "Linux" | "Windows")
case "$distro" in seconds="$(< /proc/uptime)"
*"Puppy"* | "Quirky Werewolf"* | "Alpine Linux"* | "OpenWRT"* | "Windows"*) seconds="${seconds/.*}"
uptime="$(uptime | awk -F ':[0-9]{2}+ |(, ){1}+' '{printf $2}')"
;;
"openSUSE"*)
uptime="$(uptime | awk -F ':[0-9]{2}+[a-z][a-z] |(, ){1}+' '{printf $2}')"
;;
*)
uptime="$(uptime -p)"
[ "$uptime" == "up " ] && uptime="up $(awk -F'.' '{print $1}' /proc/uptime) seconds"
;;
esac
;; ;;
"Mac OS X" | "iPhone OS" | "BSD") "Mac OS X" | "iPhone OS" | "BSD")
# Get boot time in seconds
boot="$(sysctl -n kern.boottime)" boot="$(sysctl -n kern.boottime)"
boot="${boot/'{ sec = '}" boot="${boot/'{ sec = '}"
boot="${boot/,*}" boot="${boot/,*}"
# Get current date in seconds # Get current date in seconds
now="$(date +%s)" now="$(date +%s)"
uptime="$((now - boot))" seconds="$((now - boot))"
# Convert uptime to days/hours/mins
minutes="$((uptime / 60%60))"
hours="$((uptime / 3600%24))"
days="$((uptime / 86400))"
case "$minutes" in
1) minutes="1 minute" ;;
0) unset minutes ;;
*) minutes="$minutes minutes" ;;
esac
case "$hours" in
1) hours="1 hour" ;;
0) unset hours ;;
*) hours="$hours hours" ;;
esac
case "$days" in
1) days="1 day" ;;
0) unset days ;;
*) days="$days days" ;;
esac
[ "$hours" ] && \
[ "$minutes" ] && \
hours+=","
[ "$days" ] && \
[ "$hours" ] && \
days+=","
uptime="up $days $hours $minutes"
;; ;;
"Solaris") "Solaris")
uptime="$(uptime | /usr/xpg4/bin/awk -F ':[0-9]{2}+[a-z][a-z] |(, ){1}+' '{printf $2}')" seconds="$(kstat -p unix:0:system_misc:snaptime | awk '{print $2}')"
seconds="${seconds/.*}"
;; ;;
esac esac
days="$((seconds / 60 / 60 / 24)) days"
hours="$((seconds / 60 / 60 % 24)) hours"
minutes="$((seconds / 60 % 60)) minutes"
case "$days" in
"0 days") unset days ;;
"1 days") days="${days/s}" ;;
esac
case "$hours" in
"0 hours") unset hours ;;
"1 hours") hours="${hours/s}" ;;
esac
case "$minutes" in
"0 minutes") unset minutes ;;
"1 minutes") minutes="${minutes/s}" ;;
esac
uptime="${days:+$days, }${hours:+$hours, }${minutes}"
uptime="${uptime%', '}"
uptime="up ${uptime:-${seconds} seconds}"
# Make the output of uptime smaller. # Make the output of uptime smaller.
case "$uptime_shorthand" in case "$uptime_shorthand" in
"on") "on")
@ -353,7 +332,7 @@ getuptime() {
uptime="${uptime/ minutes/m}" uptime="${uptime/ minutes/m}"
uptime="${uptime/ minute/m}" uptime="${uptime/ minute/m}"
uptime="${uptime/ seconds/s}" uptime="${uptime/ seconds/s}"
uptime="${uptime/,}" uptime="${uptime//,}"
;; ;;
esac esac
} }