2014-01-21 shell脚本练习题

回复 收藏
咱们学过如何查看进程了,今天我让大家写个这样的需求:
在服务器上,写一个监控脚本,每隔10s去检测一次服务器上的httpd进程数,如果大于等于500的时候,就需要自动重启一下apache服务,并检测启动是否成功?若没有正常启动还需再一次启动,最大不成功数超过5次则需要理解发邮件通知管理员,并且以后不需要再检测! 如果启动成功后,1分钟后再次检测httpd进程数,若正常则重复之前操作(每隔10s检测一次),若还是大于等于500,那放弃重启并需要发邮件给管理员,然后自动退出该脚本。
2014-01-21 14:44 举报
已邀请:
0

Louis

赞同来自:

本帖最后由 Louis 于 2014-8-31 12:18 编辑

#!/bin/bash
## This script is for checking if website connected amount greater than 500,if yes send email,if no continue monitor.
## Writed by Louis on 2014/08/31 10:45

ips=`netstat -an|grep '192.168.0.101:80'|wc -l`         #The ip 192.168.0.101 here is my own ip.Please change ip when you want to run this script.

while :; do
    if [ $ips -ge 3 ]; then                                      #The 3 here is for testing,if really run this script,change 3 to 500.same as below.
        /usr/local/apache2/bin/apachectl restart &>> restart.log
        m=`ps aux|grep -v 'grep'|grep -q httpd`
        if  $m; then
            sleep 60
            ips=`netstat -an|grep '192.168.0.101:80'|wc -l`           #The $ips here defined again is for refresh it's value,for next part.
            if [ $ips -ge 3 ]; then
                mail -s 'Httpd connect amount check' root@localhost < restart.log &>> restart.log          #The email root@localhost here is for testing.Please change email before runing script.same as below.
                exit
            else
                continue
                ips=`netstat -an|grep '192.168.0.101:80'|wc -l`
            fi
        else
        n=1
        while ! $m; do
            /usr/local/apache2/bin/apachectl restart &>> restart.log
            n=$[$n+1]
            if [ $n -le 5 ] && ! $m; then
                continue
                ips=`netstat -an|grep '192.168.0.101:80'|wc -l`
            elif $m; then
                sleep 60
                ips=`netstat -an|grep '192.168.0.101:80'|wc -l`
                while :; do
                    if [ $ips -ge 3 ]; then
                        echo "httpd restarted,but connect amount is still great than 500."|mail -s 'Httpd connect amount check' root@localhost < restart.log &>> restart.log
                        exit
                    else
                        sleep 10
                        ips=`netstat -an|grep '192.168.0.101:80'|wc -l`
                    fi
                done
            else
                mail -s 'Httpd connect amount check' root@localhost < restart.log &>> restart.log
                exit
            fi
        done
    fi
    else
        sleep 10
        ips=`netstat -an|grep '192.168.0.101:80'|wc -l`
    fi
done

PS:我写脚本,怎么都这么臃肿呢!不过,功能没有问题。
共同的操作处,可以做函数,不过也就一句“ips=`netstat -an|grep '192.168.0.101:80'|wc -l`”,不写也罢;判断语句条件if [ $ips -ge 3 ]多处相同,可具体操作不同 ,就不会写函数了。
得出结论:shell脚本,继续多练、多看别人是怎么写的...

0

huifeidexiaxia

赞同来自:

这么复杂啊
0

cmzsteven

赞同来自:

#!/bin/bash
while :; do
    n=`ps aux |grep httpd|grep -v grep|wc -l`
    if [ $n -ge 500 ]; then
        /usr/local/apache2/bin/apachectl restart
        sleep 10
        started_num= 0

        while [ $started_num -ge 0 ]; do
            if [ $started_num -gt 5 ]; then
                echo "httpd service start failure!" | mail -s "httpd service start failure!" abc@139.com
                exit
            fi

            process_num=`ps aux |grep httpd|grep -v grep|wc -l`
            if [ $process_num -eq ""]; then
                /usr/local/apache2/bin/apachectl start
                start_num=`expr $start_num + 1`
            else
                sleep 60
                process_num=`ps aux |grep httpd|grep -v grep|wc -l`
                if [ $process_num -gt 500 ]; then
                    echo "httpd service is unnormal!" | mail -s "httpd service is unnormal!" abc@139.com
                    exit
                fi
            fi
        done
    fi
    sleep 10
done
0

hangtiangazi

赞同来自:

本帖最后由 hangtiangazi 于 2015-3-20 12:23 编辑

代码


  1. #!/bin/bash
  2. ## test by gxw(参考六期 昌明智和四期 彭国锋脚本 )
  3. successsum=0;
  4. while :; do
  5.   httpdnum=`ps aux | grep "httpd"|grep -v "grep"|wc -l `
  6.   restartnum=0;
  7.   if [ httpdnum - lt 5000 ];then
  8.                 if[ $successsum- eq 1 ];then
  9.                         echo "宕机了"|mail -s "完了" 123#123.com
  10.                         exit
  11.                 fi
  12.           while [ restartnum -le 5 ];do
  13.                  /usr/local/apache2/bin/apachectl restart
  14.                  restartnum=$[$restartnum+1]
  15.                  ps aux |grep "httpd "| grep -v  "grep" >>  1.txt
  16.                  if grep -q 'httpd'  1.txt ;then
  17.                         successnum=$[$successnum+1]
  18.                         break
  19.                  fi
  20.           done
  21.           if [ restartnum -eq 6 ];then
  22.              echo "重启次数超过了五次"|mail -s "不行啦!" 123@123.com
  23.                  exit
  24.           else
  25.                  sleep 60
  26.           fi
  27.         fi
  28. done

这段代码中successsum=0代表成功启动的次数,restartnum代表的启动的次数。看起来很复杂,只要理清思路就没有什么问题了。欢迎探讨。

0

hangtiangazi

赞同来自:


谢谢大家多多探讨。。
0

Armani

赞同来自:

while :
do
aa=`ps aux | grep httpd | grep -v grep  | wc -l`
  if  [  $aa  -ge  500 ];then
      /etc/init.d/httpd restart
      for i in  `seq 1 6`
       do
          if [ "$i" -gt 5 ];then
           mail  -s   "Down!"   xxxx@163.com
           break
          fi

            netstat -lnp | grep httpd  >  /dev/null
           if [ $? -ne  0 ];then
                /etc/init.d/httpd restart
             else
              break
           fi
       done
   fi
   echo OK
   sleep 60
   bb=`ps aux | grep httpd | grep -v grep  | wc -l`
     if  [  $bb  -ge  500 ];then
         mail  -s   "Down!"   xxxx@163.com
     fi
    sleep 10
done
0

307141950

赞同来自:

k=0
while :
do
        sleep 10
        if `ps aux | grep httpd |wc -l` -ge 500
        then
                for k in `seq 0 4`
                do
                        if /etc/init.d/httpd restart
                        then
                                sleep 60
                                if [ `ps aux | grep httpd |wc -l` -gt 500 ]
                                then
                                        mail "zhuti" "neirong" 123@qq.com
                                else
                                        break
                                fi
                        fi
                done

                if [ k -eq 4]
                then
                        mail "zhuti" "neirong" 123@qq.com
                        exit
                fi
        fi
done

0

翟厚翔

赞同来自:

while : ; do  n=1 n=`ps -ef|grep http |wc -l`   if [ $n -gt 500 ] ;   then   /etc/init.d/httpd restat > /dev/null 2>/dev/null m=`ps -ef|grep httpd |grep -v grep|wc -l`   while [ $m -eq 0 ];  do    /etc/init.d/httpd restat > /dev/null 2>/etc/httpd_restat.err     m=`ps -ef|grep httpd |grep -v grep|wc -l`       if [ $n -eq 5 ];        then          echo " httpd is down " mail -s|1357970430@qq.com < httpd_restart.err;          exit          else          n=$n+1             break         fi       done fi  sleep 60 b=`ps -ef|grep http |wc -l`    if [ $b -gt 500 ] ;   then echo " httpd is down " mail -s|1357970430@qq.com < httpd_restart.err; fi done
0

J!_yuan

赞同来自:

  1. #!/bin/bash
  2. #****************************************8
  3. #尝试启动5次
  4. #5次之后 不成功就发邮件。并退出循环
  5. function try(){
  6.                         a=0
  7.                         exec netstat -lnp|grep httpd > /dev/null
  8.                         run=`echo $?`
  9.                         while [ ! $run -eq 0 ];do

  10.                         /etc/init.d/httpd restart 2>/tmp/start_error.log
  11.                         netstat -lnp|grep httpd >dev/null
  12.                         run=`echo $?`
  13.                         a=$[$a+1]
  14.                         if [ $a -eq 5 ];then
  15.                         a=0
  16.                         mail -s httpd_down 996156275@qq.com </tmp/start_error.log
  17.                         echo "httpd start fail!!!"
  18.                         return 1
  19.                         break
  20.                         fi
  21.                         done
  22.                         return 0
  23. }
  24. try
  25. echo "the httpd server exist!!"
  26. while :;do
  27. process=$[`ps aux | grep httpd |wc -l`-2]
  28.          if [ $process -ge 500 ];then
  29.                 try
  30.                 if [ `echo $?` -eq 1];then
  31.                         break
  32.                         elif [ `echo $?` -eq 0];then
  33.                                 sleep 60
  34.                                 process=$[`ps aux | grep httpd |wc -l`-2]
  35.                                 if [$process -ge 500];then
  36.                                 echo "process more than 500"|mail -s httpd_process_morethan 500 9961562@qq.com
  37.                                 exit 0
  38.                                 fi
  39.                 fi
  40.         fi
  41. sleep 10
  42. done
  43. 各路大神看看 有什么问题吗
0

石头

赞同来自:

#!/bin/bash
n=`ps aux|grep nginx|grep -v grep|wc -l`
gO() {
while :; do
        if [ "$n" -ge "500" ];then
        echo
        else
            echo "Advanced fault" |mail -s "httpd service start failure!" abc@139.com
            exit
        fi
        sleep 10
done
}
i=0
while : ;do
   if  [ "$n" -ge "500" ];then
       sleep 10
        else
        /usr/local/nginx/sbin/nginx -s reload
        if [ $? -ne 0 ];then
        let i=$i+1
        /usr/local/nginx/sbin/nginx -s reload
        else
        gO
        fi
        fi
        if [ $i -ge 5 ];then
        echo "Restart times more than five times" |mail -s "httpd service start failure!" abc@139.com
         exit
     fi
done
试了试应该没问题。我不会发邮件复制的别人的。

0

rolay8

赞同来自:

这个有点挑战难度,试试看
0

hlymlv

赞同来自:

这样写能行?
#!/bin/bash
i=0
number=`ps aux |grep 'httpd'|grep -v 'grep'|wc -l`
check(){
if [ $? -eq 0 ];then
exit
else
/usr/local/apache/bin/apachectl restart
fi
}
until [ $i=5 ]
do
i=`expr $i + 1`
if [ $number -gt 500 ];then
/usr/local/apache/bin/apachectl restart
check
fi
done
if [ $? -ne 0 ];then
echo "be failed,need help"|mail -s "httpd" 123@321.com
else
sleep 10
fi
crontab -e
*/1 * * * * sh 1.sh
0

loveyouhyf

赞同来自:

本帖最后由 loveyouhyf 于 2016-1-7 21:55 编辑

初步写出来的,请各位测试并指正!
#!/bin/bash
##write by 2016-01-07
while :;do
a=`ps aux|grephttpd|grep -v grep|wc -l`
if [ $a -gt 500 ];then
/usr/local/apache/bin/apachectlrestart
b=`echo $?`
while [ $b -ne 0 ];do
i=$[$i+1]
if [ $i -eq 5 ];then
echo "the httpdservice have already start failed five!" | mail -s"www.com.cn failed" abc@139.com
exit
fi
/usr/local/apache/bin/apachectlrestart
b=`echo $?`
done
if [ $b -eq 0 ];then
sleep 60
continue
fi
else
sleep 10
fi
done
0

415222090

赞同来自:

真不清楚,学习下
0

王sir

赞同来自:

收藏一下,晚上写一个看看
0

gxp2008

赞同来自:

本帖最后由 gxp2008 于 2016-3-16 18:43 编辑
  1. #!/bin/bash
  2. while :
  3. do
  4.      a=`ps -ef |grep httpd |wc -l`
  5.      if [ $a -gt 500 ]
  6.      then
  7.          /etc/init.d/httpd start
  8.          cu=`curl -s -I localhost |grep HTTP|awk '{print $2}'`
  9.             for i in `seq 1 5`
  10.             do
  11.                 if [ $cu -ne 200 ]
  12.                 then
  13.                      /etc/init.d/httpd start
  14.                 else
  15.                      echo "宕机了"|mail -s "完了" lz_gxp@126.com
  16.                      exit
  17.                 fi
  18.             done
  19.      else
  20.          sleep 10
  21.      fi
  22. done


0

zmh0415

赞同来自:


[ -f `netstat -lnp |grep -q  ":::80"` ] || /usr/sbin/apachectl start
a=`ps aux |grep httpd |wc -l`
if [ a >= 500 ];
then /usr/sbin/apachectl restart
      [ -f `netstat -lnp |grep -q  ":::80"` ] || /usr/sbin/apachectl start
      fi
学习先
0

keluo

赞同来自:

学习 有点晕
0

lerchi

赞同来自:

学习
0

lerchi

赞同来自:

学习
0

阿凯

赞同来自:

本帖最后由 阿凯 于 2016-5-3 10:29 编辑
  1. while : ; do
  2. m=`ps aux |grep -v 'grep' |grep httpd |wc -l`
  3. if [ $m -lt 6 ]
  4. then
  5.         sleep 10
  6.         continue
  7. else
  8.         /etc/init.d/httpd restart
  9.         n=1
  10.         while [ $? -ne 0 ]
  11.         do
  12.                  /etc/init.d/httpd restart
  13.                  n=$[$n+1]
  14.         if [ $n > 5 ]
  15.         then
  16.                   mail -s "The httpd service start failure" 123@qq.com
  17.                   break
  18.         fi
  19.         done
  20.         if [ $? -eq 0 ] ;then
  21.                 sleep 60
  22.                 if [ $m -lt 6 ] ;then
  23.                         sleep 10
  24.                         continue
  25.                 else
  26.                         mail -s "The httpd server is stop,Please check" 123@qq.com
  27.                         exit
  28.                 fi
  29.         fi
  30. fi
  31. done












0

qq495966654

赞同来自:

一脸懵逼{:7_206:}{:7_175:}
0

CNS2016

赞同来自:

学习
0

branttsai

赞同来自:

study,tks
0

标哥

赞同来自:

#!/bin/bash
while :
do
    value=`ps aux |grep -v grep|grep httpd|wc -l`
    if [ $value -gt 500 ]
    then
       /usr/local/apache2/bin/apachectl restart
       flag=`netstat -lnp|awk '{print $4}'|awk -F':'  '{print $NF}'|grep ^[0-9]|grep -w '80' `
       if [ -n "$flag" ]
       then
           sleep 60
           continue
       else
         i=0
         while [ -z "$flag" && i -le 5 ] ;do
            /usr/local/apache2/bin/apachectl restart
            flag=`netstat -lnp|awk '{print $4}'|awk -F':'  '{print $NF}'|grep ^[0-9]|grep -w '80' `
            i=$[ $i + 1 ]
         done
         if [ i -gt 5];then
           echo "apache restart > 5 numbers"| mail -s "warning"  15769162764@163.com
         fi

       fi


   fi
   echo "sleep 10s..."
   sleep 10
done

不知道这样写对不对,还请老师指点.

0

dongteng

赞同来自:

学习
0

dongteng

赞同来自:

学习
0

dessler

赞同来自:

我越写逻辑越绕了
0

sun330

赞同来自:

学习

0

大雁

赞同来自:

铭哥怎么没给答案啊

0

fy88fy

赞同来自:

学习

回复帖子,请先登录注册

退出全屏模式 全屏模式 回复
评分
可选评分理由: