0

I'd like to quickly get uptime for mysql in a useful way, not just dump the seconds from GLOBAL STATUS and then do some math. Is there a way to accomplish this on Linux?

flickerfly
  • 2,753
  • 3
  • 25
  • 27

1 Answers1

1

This little script dumps uptime in the format: "1 day 4 hours 35 minutes". It only lists seconds when that is the only current value over 0 and days when uptime is over 1 day.

#/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
flickerfly
  • 2,753
  • 3
  • 25
  • 27