2014-10-17 shell脚本练习题

回复 收藏
第四期的兄弟姐妹一共有50来个,可学的出类拔萃的就那么几个,这个是个永恒不变的定理,就跟咱们上学的时候一样,一个班里不可能个个都是尖子生,除非是特训班。其实,我非常想把大家都培养成尖子生。但事实上,那是一个巨大的工程,甚至比中个百万大奖都难! 但,我会竭尽全力尽为大家铺好路,在成功的路上,一路为你保驾护航!

今日shell练习: 假如你服务器上跑的是httpd,那请写一个监控脚本,每分钟检查一次httpd进程是否存在,若是发现不在了,那么就启动它。(写脚本很简单,明白了思路,然后就是动手去写,先实现需求,等需求实现了,再去补充更好的功能,比如输出漂亮的日志、漂亮的格式、更完善的逻辑)
2014-10-17 09:23 举报
已邀请:
0

游夜

赞同来自:

本帖最后由 游夜 于 2014-10-17 10:14 编辑

先把基本的写出来。出差在外,时间不好分配
#!/bin/bash
#shell name check_httpd.sh
#shell dir /usr/local/src/check_httpd.sh
service=`netstat -lnp |grep httpd|awk -F '/' '{print $2}'`
if [ "$service" != "httpd" ]
then
{
/usr/local/apache2/bin/apachectl start
echo "httpd start!"
}
fi
加入任务计划,每分钟执行一次
crontab -e
*/1 * * * * root ./temp.sh


0

齐天大圣

赞同来自:

本帖最后由 齐天大圣 于 2014-10-17 10:21 编辑
  1. #!/bin/bash
  2. while :;
  3. do
  4.   [ -z `ps aux |grep httpd |grep -v httpd|awk '{print $11}'|sort -u`] && /usr/local/apache2/bin/apachectl start && echo "`date +%F%T` httpd is down and already start!plz check error_log." |mail -s httpd xxx@xxx.com
  5.   sleep 60
  6. done
0

coffee

赞同来自:

  1. #!/bin/bash
  2. #This is httpd daemon script

  3. while true
  4. do
  5.         web=`pgrep httpd | wc -l`
  6.         if [ $web -eq 0 ];then
  7.                 /usr/local/apache2/bin/apachectl start
  8.         fi

  9. done

  10. 计划任务
  11. crontab -e
  12. * * * * * /root/web.sh



0

小雨

赞同来自:

本帖最后由 小雨 于 2014-10-17 10:55 编辑

  1. #!/bin/bash


  2. while :; do
  3.         a=`netstat -lnp |grep httpd`
  4.         if [ -z $a ]; then
  5.                 echo "sercice has stop"
  6.                 /usr/local/apache2/bin/apachectl start
  7.         else
  8.                 echo "service is running"
  9.         fi
  10.       
  11.         sleep 60
  12. done        



0

soar

赞同来自:

本帖最后由 soar 于 2014-10-17 10:59 编辑

#!/bin/bash
#test system's httpd services
echo "Now ,the  httpd service will  to be detcted...";
httpd=`/usr/bin/pgrep httpd`
if [ "$httpd" != "" ]
then
        echo "The httpd service is running"
else
        echo "The httpd service is not running,now start to run it"
        /usr/local/apache2/bin/apachectl start
fi

然后放到任务计划,每分钟执行一次即可
0

ocean

赞同来自:

  1. # cat 1017.sh
  2. #!/bin/bash
  3. # Desc : this script is to check HTTP services Healthy,if not then restart.
  4. # Writen by Jeffery.Su
  5. # Date:  Oct,17 2014

  6. PRO_NUM=`ps -A | grep httpd|wc -l`
  7. DATE=`date +"%Y-%m-%d %H:%M"`
  8. LOG_PATH=/tmp/httpd_check/`date +%Y%m`
  9. LOG_FILE=$LOG_PATH/httpd_check_`date +%d`.log

  10. [ -d $LOG_PATH ] || mkdir -p $LOG_PATH

  11. if [ $PRO_NUM -eq 0 ];then
  12.      /usr/local/apache2/bin/apachectl start
  13.      echo `date +'%Y/%m/%d %H:%M'` httpd err >> $LOG_FILE
  14. else
  15.      echo `date +'%Y/%m/%d %H:%M'` httpd ok >> $LOG_FILE
  16. fi


  17. echo "*/1 * * * *  /bin/bash /usr/local/bin/1017.sh" >>/var/spool/cron/root
0

jaws1689

赞同来自:

  1. #!/bin/bash
  2. #20141017_practice

  3. while :
  4.   do
  5.    server=`netstat -lnp | grep httpd | awk -F/ '{print $2}'`
  6.    if [ -z $server ]
  7.      then
  8.       /usr/local/apache2/bin/apachectl restart
  9.      else
  10.        sleep 60
  11.    fi
  12. done
0

chenzudao1234

赞同来自:

#!/bin/bash
while true
do
   ps aux | grep httpd>1.txt
   a=`cat 1.txt | wc -l`
   if [ $a -eq 1 ]
    then
       /usr/local/apache/bin/httpdctl graceful
   fi

   sleep 60
done
0

So Long

赞同来自:

本帖最后由 程城 于 2014-10-17 19:48 编辑

  1. #/bin/bash
  2. #This  shell scripts  is  to  check the apache service run or  down. If apache is down  turn is run.

  3. while : ;  do

  4. if_run=`ps  aux |grep  httpd |awk '{print $7}'|sed  's#^.*/##g'`
  5. if [ $if_run   -ne  "httpd"  ] ;then
  6.        service  httpd start        
  7. else
  8.        echo "apache service have been run ago !"

  9. fi
  10.    sleep   60
  11. done
谢谢提醒,我太粗心了。
0

陈沛

赞同来自:

  1. #!/bin/bash
  2. # 2014-10-17
  3. # chenpei
  4. # If the httpd start

  5. while :;
  6. do
  7.     if [ -z "`netstat -lnp | grep httpd`" ]; then
  8.         /usr/local/apache2/bin/apachectl start
  9.         echo "start httpd service."
  10.     fi
  11.     sleep 60
  12. done
0

coffee

赞同来自:

本帖最后由 coffee 于 2014-10-17 16:05 编辑

程城你的while循环  少了  do
0

zyfeifie

赞同来自:

本帖最后由 zyfeifie 于 2014-10-17 18:33 编辑
  1. #!/bin/bash
  2. time=`date +%F-%H:%M:%S`
  3. while : ;
  4. do
  5. num=`ps -ef | grep httpd| grep -v 'grep' | wc -l`
  6. if [ $num -eq 0 ]
  7. then
  8. echo $time >>/tmp/err.log
  9. /usr/local/apache2/bin/apachectl start >>/tmp/err.log
  10. fi
  11. sleep 60
  12. done
0

Louis

赞同来自:

  1. #!/bin/bash
  2. ## This script is for check if service httpd is on,if no,restart service.
  3. ## Written by Louis at 2014/10/17 19:06

  4. log=/tmp/httpdcheck.log
  5. while :; do
  6.     if ! ps aux|grep -v grep|grep -q httpd; then
  7.         echo "[`date +'%F %T'`]: httpd service is off" >> $log
  8.         /usr/local/apache2/bin/apachectl start &>> $log
  9.         if ! ps aux|grep -v grep|grep -q httpd; then
  10.             mail -s 'httpd start fialed.please check.' root@localhost < $log
  11.             exit
  12.         fi
  13.     fi
  14. sleep 60
  15. done


思路:ps aux查看是否已有httpd进程,如果没有,记录时间,并启动httpd服务;启动后,再次判断,如果还启动不成功,发送邮件告警,人工查看启动失败原因,并且退出死循环。第一次判断httpd进程如果存在或者启动成功,均不记录信息。
0

So Long

赞同来自:

coffee 发表于 2014-10-17 15:54
程城你的while循环  少了  do

谢谢提醒,{:4_94:}
0

陌懒

赞同来自:

本帖最后由 陌懒 于 2014-10-20 17:00 编辑
  1. #!/bin/bash
  2. test=$(netstat -tuln|grep ":80")

  3. if [ "$test" == "" ];then
  4.       echo "ERROR_`date +%F_%T`      The httpd has down!">>/tmp/http.log
  5.       /usr/local/apache2/bin/apachectl start

  6. else
  7.      echo "CORRECT_`date +%F%T`    Httpd is running!">>/tmp/http.log
  8. fi

  9. 加入计划任务
  10. crontab -e
  11. * * * * * /root/checkhttpd.sh

0

平弟

赞同来自:

不会啊   感觉不太好
0

sss

赞同来自:

  1. #!/bin/bash
  2. #Checked every minute httpd process exists
  3. #If the discovery is gone, then start it
  4. #2015/05/12

  5. services=`netstat -nltp | grep httpd | awk -F '/' '{print $2}'`

  6. if [ "$services" != httpd ]
  7.         then
  8.         /usr/local/apache2/bin/apachectl start
  9.         echo "httpd start !"
  10. fi


0

佳生-Jason

赞同来自:

#! /bin/bash
##
##

log=/var/log/httpdcheck.log
while :; do
    ​a=`ps aux |grep httpd |wc -l`
    if [ $a -eq 0 ]; then
        echo "[`date +'%F %T'`] httpd service is off.Try to start now" >>$log
        /usr/local/apache2/bin/apachectl start >> $log
    ​else
    ​    ​echo "httpd service is running."
    ​    ​exit
    ​fi   
    ​b=`netstat -lnp |grep httpd |wc -l`
    ​​if [ $b -eq 0 ]; then
        mail -s 'Htttpd service start fialed. Please check.' 1091322612@qq.com < $log
    ​    ​echo "[`date +'%F %T'`]httpd service start fialed. please check." >>$log
        exit
    else
    ​    ​echo "[`date +'%F %T'`] httpd service is started successfully." >>$log
    ​fi
    sleep 60
done
0

翟厚翔

赞同来自:

#!/bin/bash
SERV=`ps -ef|grep 'httpd'|grep -v 'grep'|wc -l`
if [ $SERV -eq 0 ];then
   service httpd start &> /dev/null
   sleep 10
   SERV1=`ps -ef|grep 'httpd'|grep -v 'grep'|wc -l`
    if [ $SERV1 -eq 0 ];then
         service httpd start 1>/dev/null 2>httpd_err.log
       else
         echo "httpd start is success"
    fi
else
       echo "httpd is exits"
fi
0

prospect

赞同来自:

#!/bin/sh  while true do     web=`pgrep httpd |wc -l`     if [ $web -eq 0 ];then         /usr/local/apache2/bin/apachectl start     fi sleep 60 done
0

simle

赞同来自:

1 #!/bin/bash
  2 #It's created by chaochao
  3 `ps aux |grep httpd > httpd.txt`
  4 grep '/usr/local/apache2/bin/httpd -k start' httpd.txt
  5 sign=`echo $?`
  6 if [ $sign == 0  ];then
  7 echo "httpd is running"
  8 else
  9 echo "It begins httpd.sh:!!!"
10 echo "It begins httpd.sh:!!!"
11 /usr/local/apache2/bin/apachectl start
12 fi
13 exit


0

loveyouhyf

赞同来自:

#!/bin/bash
while :;do
if [  `pgrep 'httpd'|wc -l` -gt 1 ];then
echo "your httpd service is ok!"
sleep 60
else
for i in `pgrep 'httpd'`;do
kill -9 $i
done
/usr/local/apache/bin/apachectl restart
fi
done
0

大仔黑黑

赞同来自:

#!/bin/bash
##written by wangyl
##2016-03-22

while :;
do
        a=`netstat -lnp | grep "httpd"`
        if [ -z $a ];then
                /etc/init.d/httpd start
                echo "server httpd was done,please start httpd right now" >> /tmp/httpd.log
        fi
        #sleep 60
done
0

kevinjin

赞同来自:

  1. #! /bin/bash
  2. if ps aux|grep httpd|grep -v grep|grep -q httpd
  3. then
  4.     echo "httpd is running"
  5. else
  6.     echo "httpd is not running, start it"
  7.     /usr/local/apache2/bin/apachectl start
  8. fi

  9. crontab -e
  10. */1 * * * * sh ps_httpd.sh
0

大雁

赞同来自:

ps aux |grep httpd|grep -v grep >/dev/null
 if [ $? -eq 0 ]
 then
   echo "httpd is runing"
 else
   echo "httpd is downing,try run the httpd"
   /usr/local/apache2/bin/apachectl start >/dev/null
fi

0

王斌

赞同来自:

#! /bin/bash

# Check service httpd is running or not, if not, start httpd.

# auth : Cityzen Wang

httpstat=`ps aux |grep httpd|grep -v "grep httpd"`

if [ -n "$httpstat" ]

then

    echo "httpd is running"

else

    /usr/local/apache2/bin/apachectl start

fi

* * * * * /bin/sh /usr/local/sbin/2.sh

0

杨银根

赞同来自:

#!/bin/bash

h=`ps aux|grep httpd|wc -l`

while :

do

   if [ $h -le 1 ]

   then

      /etc/init.d/httpd start

   fi

   sleep 60

done

回复帖子,请先登录注册

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