0

在我的用于服务器监控的自定义 bash 脚本中,它实际上是为了强制我的 CentOS 服务器采取一些措施并在资源过载的时间超过预期时提醒我,我收到以下错误

第 17 行:[:5.74:预期整数表达式 *

现在根据定义,所有 iostat 结果都是浮点数,我已经awk在我的 iostat 命令 (WAIT) 中使用过,所以我怎样才能让我的 bash 脚本期望一个而不是整数?

** 值 5.74 代表当前 iostat 结果

#!/bin/bash

if [[ "`pidof -x $(basename $0) -o %PPID`" ]]; then
#       echo "Script is already running with PID `pidof -x $(basename $0) -o %PPID`"
        exit
fi

UPTIME=`cat /proc/uptime | awk '{print $1}' | cut -d'.' -f1`
WAIT=`iostat -c | head -4 |tail -1 | awk '{print $4}' |cut -d',' -f1`
LOAD=`cat /proc/loadavg |awk '{print $2}' | cut -d'.' -f1`

if [ "$UPTIME" -gt 600 ]
then
        if [ "$WAIT" -gt 50 ]
        then
                if [ "$LOAD" -gt 4 ]
                then
                        #action to take (reboot, restart service, save state sleep retry)
                        MAIL_TXT="System Status: iowait:"$WAIT" loadavg5:"$LOAD" uptime:"$UPTIME"!"
                        echo $MAIL_TXT | mail -s "Server Alert Status" "mymail@foe.foe"
                        /etc/init.d/httpd stop
#                       /etc/init.d/mysql stop
                        sleep 10
#                       /etc/init.d/mysql start
                        /etc/init.d/httpd start
                fi
        fi
fi

CentOS release 6.8 (Final) 2.6.32-642.13.1.el6.x86_64

4

1 回答 1

1

通常,您需要使用本机 shell 数学以外的其他东西,如BashFAQ #22中所述。但是,由于您要与整数进行比较,所以这很容易:您可以在小数点处截断。

[ "${UPTIME%%.*}" -gt 600 ] # truncates your UPTIME at the decimal point
[ "${WAIT%%.*}" -gt 50 ]    # likewise
于 2017-02-10T17:00:40.167 回答