Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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
share|improve this question
1  
Why do you post a completely different version after I cleaned the first one up? And more important: Why don't you tell us what kind of error reports you get when running this script? –  SvW Nov 2 '11 at 13:08
7  
Rebooting a server when there is a problem isn't really a solution. It would be better to know what is causing the problem and acting accordingly. –  Bart De Vos Nov 2 '11 at 13:19
    
because the first version is for debian not CentOS,it was my overlook,so I amend it." –  user167043 Nov 2 '11 at 13:29
7  
In general you don't reboot a server when things are acting nuts. You figure out what's wrong, and fix that problem. It's kind of the difference between fixing the hole in a tire and pumping air into it every two days...they both "work" but one is obviously better as a solution rather than a workaround. –  Bart Silverstrim Nov 2 '11 at 13:48
3  
@user167043 <Lecture> The core principle of system administration (or any engineering practice) is to fix the real problem, not flail around wildly (in your case restarting services) because you don't know what's causing the problem. Do yourself and your employer a favor: Find and fix the REAL issue, or at least thoroughly understand WHY you're restarting services (and tell us when you ask for help). Simply bouncing systems/services because you hit an arbitrary memory or load threshold is not generally considered to be an acceptable way of managing systems. </Lecture> –  voretaq7 Nov 2 '11 at 14:19
add comment

closed as not constructive by ceejayoz, Scott Pack, Bart De Vos, Shane Madden, EEAA Nov 3 '11 at 2:54

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers

Take a look at monit, it seems to be capable of what you need and more.

share|improve this answer
    
I would like to use script,as I think monit is complicated to setup –  user167043 Nov 2 '11 at 13:12
1  
Monit takes about five minutes to set up. –  ceejayoz Nov 2 '11 at 13:24
    
@ceejayoz why did you vote down ? what was wrong with this question do you think ? –  user167043 Nov 2 '11 at 13:26
1  
Who says I'm the one who voted it down? Votes are anonymous. –  ceejayoz Nov 2 '11 at 13:42
2  
Also, too lazy to follow up on suggestions -- –  adaptr Nov 2 '11 at 13:46
show 1 more comment

This script doesn't automatically reboot Centos 5 server on high memory usage or high cpu.. It restart the services you specify in. Here is the corrected version for 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
share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.