第四期的兄弟姐妹一共有50来个,可学的出类拔萃的就那么几个,这个是个永恒不变的定理,就跟咱们上学的时候一样,一个班里不可能个个都是尖子生,除非是特训班。其实,我非常想把大家都培养成尖子生。但事实上,那是一个巨大的工程,甚至比中个百万大奖都难! 但,我会竭尽全力尽为大家铺好路,在成功的路上,一路为你保驾护航!
今日shell练习: 假如你服务器上跑的是httpd,那请写一个监控脚本,每分钟检查一次httpd进程是否存在,若是发现不在了,那么就启动它。(写脚本很简单,明白了思路,然后就是动手去写,先实现需求,等需求实现了,再去补充更好的功能,比如输出漂亮的日志、漂亮的格式、更完善的逻辑)
今日shell练习: 假如你服务器上跑的是httpd,那请写一个监控脚本,每分钟检查一次httpd进程是否存在,若是发现不在了,那么就启动它。(写脚本很简单,明白了思路,然后就是动手去写,先实现需求,等需求实现了,再去补充更好的功能,比如输出漂亮的日志、漂亮的格式、更完善的逻辑)
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 startecho "httpd start!"}fi加入任务计划,每分钟执行一次crontab -e*/1 * * * * root ./temp.sh
0
本帖最后由 齐天大圣 于 2014-10-17 10:21 编辑
- #!/bin/bash
- while :;
- do
- [ -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
- sleep 60
- done
0
- #!/bin/bash
- #This is httpd daemon script
- while true
- do
- web=`pgrep httpd | wc -l`
- if [ $web -eq 0 ];then
- /usr/local/apache2/bin/apachectl start
- fi
- done
- 计划任务
- crontab -e
- * * * * * /root/web.sh
0
本帖最后由 小雨 于 2014-10-17 10:55 编辑
- #!/bin/bash
- while :; do
- a=`netstat -lnp |grep httpd`
- if [ -z $a ]; then
- echo "sercice has stop"
- /usr/local/apache2/bin/apachectl start
- else
- echo "service is running"
- fi
-
- sleep 60
- done
0
本帖最后由 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
然后放到任务计划,每分钟执行一次即可
#!/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
- # cat 1017.sh
- #!/bin/bash
- # Desc : this script is to check HTTP services Healthy,if not then restart.
- # Writen by Jeffery.Su
- # Date: Oct,17 2014
- PRO_NUM=`ps -A | grep httpd|wc -l`
- DATE=`date +"%Y-%m-%d %H:%M"`
- LOG_PATH=/tmp/httpd_check/`date +%Y%m`
- LOG_FILE=$LOG_PATH/httpd_check_`date +%d`.log
- [ -d $LOG_PATH ] || mkdir -p $LOG_PATH
- if [ $PRO_NUM -eq 0 ];then
- /usr/local/apache2/bin/apachectl start
- echo `date +'%Y/%m/%d %H:%M'` httpd err >> $LOG_FILE
- else
- echo `date +'%Y/%m/%d %H:%M'` httpd ok >> $LOG_FILE
- fi
- echo "*/1 * * * * /bin/bash /usr/local/bin/1017.sh" >>/var/spool/cron/root
0
- #!/bin/bash
- #20141017_practice
- while :
- do
- server=`netstat -lnp | grep httpd | awk -F/ '{print $2}'`
- if [ -z $server ]
- then
- /usr/local/apache2/bin/apachectl restart
- else
- sleep 60
- fi
- done
0
#!/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
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
本帖最后由 程城 于 2014-10-17 19:48 编辑
谢谢提醒,我太粗心了。
- #/bin/bash
- #This shell scripts is to check the apache service run or down. If apache is down turn is run.
- while : ; do
- if_run=`ps aux |grep httpd |awk '{print $7}'|sed 's#^.*/##g'`
- if [ $if_run -ne "httpd" ] ;then
- service httpd start
- else
- echo "apache service have been run ago !"
- fi
- sleep 60
- done
0
- #!/bin/bash
- # 2014-10-17
- # chenpei
- # If the httpd start
- while :;
- do
- if [ -z "`netstat -lnp | grep httpd`" ]; then
- /usr/local/apache2/bin/apachectl start
- echo "start httpd service."
- fi
- sleep 60
- done
0
本帖最后由 zyfeifie 于 2014-10-17 18:33 编辑
- #!/bin/bash
- time=`date +%F-%H:%M:%S`
- while : ;
- do
- num=`ps -ef | grep httpd| grep -v 'grep' | wc -l`
- if [ $num -eq 0 ]
- then
- echo $time >>/tmp/err.log
- /usr/local/apache2/bin/apachectl start >>/tmp/err.log
- fi
- sleep 60
- done
0
思路:ps aux查看是否已有httpd进程,如果没有,记录时间,并启动httpd服务;启动后,再次判断,如果还启动不成功,发送邮件告警,人工查看启动失败原因,并且退出死循环。第一次判断httpd进程如果存在或者启动成功,均不记录信息。
- #!/bin/bash
- ## This script is for check if service httpd is on,if no,restart service.
- ## Written by Louis at 2014/10/17 19:06
- log=/tmp/httpdcheck.log
- while :; do
- if ! ps aux|grep -v grep|grep -q httpd; then
- echo "[`date +'%F %T'`]: httpd service is off" >> $log
- /usr/local/apache2/bin/apachectl start &>> $log
- if ! ps aux|grep -v grep|grep -q httpd; then
- mail -s 'httpd start fialed.please check.' root@localhost < $log
- exit
- fi
- fi
- sleep 60
- done
思路:ps aux查看是否已有httpd进程,如果没有,记录时间,并启动httpd服务;启动后,再次判断,如果还启动不成功,发送邮件告警,人工查看启动失败原因,并且退出死循环。第一次判断httpd进程如果存在或者启动成功,均不记录信息。
0
本帖最后由 陌懒 于 2014-10-20 17:00 编辑
- #!/bin/bash
- test=$(netstat -tuln|grep ":80")
- if [ "$test" == "" ];then
- echo "ERROR_`date +%F_%T` The httpd has down!">>/tmp/http.log
- /usr/local/apache2/bin/apachectl start
- else
- echo "CORRECT_`date +%F%T` Httpd is running!">>/tmp/http.log
- fi
- 加入计划任务
- crontab -e
- * * * * * /root/checkhttpd.sh
0
- #!/bin/bash
- #Checked every minute httpd process exists
- #If the discovery is gone, then start it
- #2015/05/12
- services=`netstat -nltp | grep httpd | awk -F '/' '{print $2}'`
- if [ "$services" != httpd ]
- then
- /usr/local/apache2/bin/apachectl start
- echo "httpd start !"
- fi
0
#! /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
##
##
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
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
#!/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
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
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
#!/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
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
##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
- #! /bin/bash
- if ps aux|grep httpd|grep -v grep|grep -q httpd
- then
- echo "httpd is running"
- else
- echo "httpd is not running, start it"
- /usr/local/apache2/bin/apachectl start
- fi
- crontab -e
- */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
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
编辑回复