Please could someone help me to correct below scripts that can automatically reboot Centos 5 server once the memory less than 85 and server load over 3. I found this script via Google,I checked it and found there are syntax errors,but I know nothing on bash script programming,so I need your help ,thanks in advance
#!/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
Answer
Take a look at monit, it seems to be capable of what you need and more.
Attribution
Source : Link , Question Author : user167043 , Answer Author : Unreason