请有人帮我纠正下面的脚本,一旦内存低于 85 并且服务器负载超过 3,该脚本就会自动重启 Centos 5 服务器。我通过谷歌找到了这个脚本,我检查了一下,发现有语法错误,但我对 bash 脚本编程一无所知,所以我需要你的帮助,提前谢谢
#!/bin/sh
#usage: */2 * * * * root /root/checkload.sh
FREE_MEM_MIN="50"
# 设置最大系统负载
SYS_LOAD_MAX="3"
RESTART_FREE_MEM_MIN="500"
NAME_LIST="httpd mysqld"
for NAME in $NAME_LIST
do
FREE_MEM=`free -m|grep Mem|awk '{print $4}'`
#FREE_MEM=`free -m|grep Mem|awk '{print $3}'`
SYS_LOAD=`uptime | awk '{print $(NF-2)}' | sed 's/,//'`
MEM_VULE=`awk 'BEGIN{print('"$FREE_MEM"'<'"$FREE_MEM_MIN"'?"1":"0")}'`
LOAD_VULE=`awk 'BEGIN{print('"$SYS_LOAD"'>='"$SYS_LOAD_MAX"'?"1":"0")}'`
#LOAD_VULE="1"
#echo $(date +"%y-%m-%d %H:%M:%S") "DEBUG $NAME" "(FREE_MEM:$FREE_MEM|$MEM_VULE,LOAD:$SYS_LOAD|$LOAD_VULE)">> /var/log/autoreboot_debug.log
if [ $MEM_VULE = 1 ] || [ $LOAD_VULE = 1 ] ;then
echo $(date +"%y-%m-%d %H:%M:%S") "killall $NAME" "(FREE_MEM:$FREE_MEM,LOAD:$SYS_LOAD)">> /var/log/autoreboot.log
service $NAME stop
sleep 3
skill $NAME
sleep 10
for i in 1 2 3
do
FREE_MEM=`free -m|grep Mem|awk '{print $4}'`
MEM_VULE=`awk 'BEGIN{print('"$FREE_MEM"'>='"$RESTART_FREE_MEM_MIN"'?"1":"0")}'`
if [ `pgrep $NAME | wc -l` -le 0 ] && [ $MEM_VULE = 1 ]
then
service $NAME start
sleep 15
echo "AutoStart:" $(date +"%y-%m-%d %H:%M:%S") "start $NAME" `ps -ef | grep $NAME | wc -l` > /var/log/autoreboot.log
fi
done
echo $(date +"%y-%m-%d %H:%M:%S") "start $NAME" "(FREE_MEM:$FREE_MEM,LOAD:$SYS_LOAD)" >> /var/log/autoreboot.log
else
MEM_VULE=`awk 'BEGIN{print('"$FREE_MEM"'>='"$RESTART_FREE_MEM_MIN"'?"1":"0")}'`
if [ `pgrep $NAME | wc -l` -le 0 ] && [ $MEM_VULE = 1 ]
then
service $NAME start
sleep 15
echo "AutoStart:" $(date +"%y-%m-%d %H:%M:%S") "start $NAME" `ps -ef | grep $NAME | wc -l` > /var/log/autoreboot.log
else
echo "$NAME very health!(FREE_MEM:$FREE_MEM,LOAD:$SYS_LOAD)" > /dev/null
fi
fi
done
答案1
看一眼监控,它似乎能够满足您的需要,甚至更多。
答案2
此脚本不会在内存使用率高或 CPU 使用率高时自动重启 Centos 5 服务器。它会重新启动您指定的服务。这是针对 GNU Bash-4.1 的更正版本:
#!/bin/bash
# Set your parameters here:
FREE_MEM_MIN="50"
SYS_LOAD_MAX="3"
RESTART_FREE_MEM_MIN="500"
NAME_LIST="httpd mysqld"
# Dont touch below this
for NAME in $NAME_LIST
do
FREE_MEM=`free -m|grep Mem|awk '{print $4}'`
SYS_LOAD=`uptime | awk '{print $(NF-2)}' | sed 's/,//'`
MEM_VULE=`awk 'BEGIN{print('"$FREE_MEM"'<'"$FREE_MEM_MIN"'?"1":"0")}'`
LOAD_VULE=`awk 'BEGIN{print('"$SYS_LOAD"'>='"$SYS_LOAD_MAX"'?"1":"0")}'`
#echo $(date +"%y-%m-%d %H:%M:%S") "DEBUG $NAME" "(FREE_MEM:$FREE_MEM|$MEM_VULE,LOAD:$SYS_LOAD|$LOAD_VULE)">> /var/log/autoreboot_debug.log
if [ $MEM_VULE = 1 ] || [ $LOAD_VULE = 1 ] ;then
echo $(date +"%y-%m-%d %H:%M:%S") "killall $NAME" "(FREE_MEM:$FREE_MEM,LOAD:$SYS_LOAD)">> /var/log/autoreboot.log
service $NAME stop
sleep 3
skill $NAME
sleep 10
for i in 1 2 3
do
FREE_MEM=`free -m|grep Mem|awk '{print $4}'`
MEM_VULE=`awk 'BEGIN{print('"$FREE_MEM"'>='"$RESTART_FREE_MEM_MIN"'?"1":"0")}'`
if [ `pgrep $NAME | wc -l` -le 0 ] && [ $MEM_VULE = 1 ]
then
service $NAME start
sleep 15
echo "AutoStart:" $(date +"%y-%m-%d %H:%M:%S") "start $NAME" `ps -ef | grep $NAME | wc -l` > /var/log/autoreboot.log
fi
done
echo $(date +"%y-%m-%d %H:%M:%S") "start $NAME" "(FREE_MEM:$FREE_MEM,LOAD:$SYS_LOAD)" >> /var/log/autoreboot.log
else
MEM_VULE=`awk 'BEGIN{print('"$FREE_MEM"'>='"$RESTART_FREE_MEM_MIN"'?"1":"0")}'`
if [ `pgrep $NAME | wc -l` -le 0 ] && [ $MEM_VULE = 1 ]
then
service $NAME start
sleep 15
echo "AutoStart:" $(date +"%y-%m-%d %H:%M:%S") "start $NAME" `ps -ef | grep $NAME | wc -l` > /var/log/autoreboot.log
else
echo "$NAME very health!(FREE_MEM:$FREE_MEM,LOAD:$SYS_LOAD)" > /dev/null
fi
fi
done