Skip to content

Instantly share code, notes, and snippets.

@flickerfly
Created October 19, 2016 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flickerfly/e77010eb0da873888f4b62522f20e5b4 to your computer and use it in GitHub Desktop.
Save flickerfly/e77010eb0da873888f4b62522f20e5b4 to your computer and use it in GitHub Desktop.
Output uptime of mysql in clean and human-readable format
#/bin/sh
# Prints the uptime of mysql
# Optional: Takes one argument for the servername, default is localhost
# Set $host
if [ $1 ]; then
host=$1
else
host="localhost"
fi
#Get Uptime in seconds from mysql server
s=`mysql -h $host --skip-column-names -e "select VARIABLE_VALUE from information_schema.GLOBAL_STATUS where VARIABLE_NAME='Uptime';"`
#Takes seconds, outputs human readable time
#Function Source: http://unix.stackexchange.com/a/170299/17808
converts()
{
local t=$1
local d=$((t/60/60/24))
local h=$((t/60/60%24))
local m=$((t/60%60))
local s=$((t%60))
if [[ $d > 0 ]]; then
[[ $d = 1 ]] && echo -n "$d day " || echo -n "$d days "
fi
if [[ $h > 0 ]]; then
[[ $h = 1 ]] && echo -n "$h hour " || echo -n "$h hours "
fi
if [[ $m > 0 ]]; then
[[ $m = 1 ]] && echo -n "$m minute " || echo -n "$m minutes "
fi
if [[ $d = 0 && $h = 0 && $m = 0 ]]; then
[[ $s = 1 ]] && echo -n "$s second" || echo -n "$s seconds"
fi
echo
}
#Convert seconds to readable time
converts $s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment