我想以一种有用的方式快速获取 mysql 的正常运行时间,而不仅仅是从 GLOBAL STATUS 转储秒数然后进行一些计算。有没有办法在 Linux 上实现这一点?
答案1
这个小脚本以“1 天 4 小时 35 分钟”的格式转储正常运行时间。当当前值大于 0 时,它仅列出秒数;当正常运行时间超过 1 天时,它仅列出天数。
#/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