Skip to content

Instantly share code, notes, and snippets.

@rany2
Created March 9, 2024 14:22
Show Gist options
  • Save rany2/692488f8738edb0d5256806fbe16e361 to your computer and use it in GitHub Desktop.
Save rany2/692488f8738edb0d5256806fbe16e361 to your computer and use it in GitHub Desktop.
Something similar to `uptime -p` but for busybox
#!/bin/sh
# pretty-print system uptime
# get uptime and idle
read -r u _ < /proc/uptime
u=${u%.*}
# function to calculate and print time period
print_period() {
period_seconds="$1"
period_label="$2"
[ "${u}" -lt "${period_seconds}" ] && return
period_value=$(( u / period_seconds ))
u=$(( u - period_value * period_seconds ))
[ "${period_value}" -gt "1" ] && period_label="${period_label}s"
printf "%d %s " "${period_value}" "${period_label}"
}
# call the function for each time period
print_period 31557600 "year"
print_period 2629800 "month"
print_period 604800 "week"
print_period 86400 "day"
print_period 3600 "hour"
print_period 60 "minute"
print_period 1 "second"
# go to the next line
printf "\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment