咱们学过如何查看进程了,今天我让大家写个这样的需求:
在服务器上,写一个监控脚本,每隔10s去检测一次服务器上的httpd进程数,如果大于等于500的时候,就需要自动重启一下apache服务,并检测启动是否成功?若没有正常启动还需再一次启动,最大不成功数超过5次则需要理解发邮件通知管理员,并且以后不需要再检测! 如果启动成功后,1分钟后再次检测httpd进程数,若正常则重复之前操作(每隔10s检测一次),若还是大于等于500,那放弃重启并需要发邮件给管理员,然后自动退出该脚本。
在服务器上,写一个监控脚本,每隔10s去检测一次服务器上的httpd进程数,如果大于等于500的时候,就需要自动重启一下apache服务,并检测启动是否成功?若没有正常启动还需再一次启动,最大不成功数超过5次则需要理解发邮件通知管理员,并且以后不需要再检测! 如果启动成功后,1分钟后再次检测httpd进程数,若正常则重复之前操作(每隔10s检测一次),若还是大于等于500,那放弃重启并需要发邮件给管理员,然后自动退出该脚本。
0
本帖最后由 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脚本,继续多练、多看别人是怎么写的...
#!/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
#!/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
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 于 2015-3-20 12:23 编辑
代码
这段代码中successsum=0代表成功启动的次数,restartnum代表的启动的次数。看起来很复杂,只要理清思路就没有什么问题了。欢迎探讨。
代码
- #!/bin/bash
- ## test by gxw(参考六期 昌明智和四期 彭国锋脚本 )
- successsum=0;
- while :; do
- httpdnum=`ps aux | grep "httpd"|grep -v "grep"|wc -l `
- restartnum=0;
- if [ httpdnum - lt 5000 ];then
- if[ $successsum- eq 1 ];then
- echo "宕机了"|mail -s "完了" 123#123.com
- exit
- fi
- while [ restartnum -le 5 ];do
- /usr/local/apache2/bin/apachectl restart
- restartnum=$[$restartnum+1]
- ps aux |grep "httpd "| grep -v "grep" >> 1.txt
- if grep -q 'httpd' 1.txt ;then
- successnum=$[$successnum+1]
- break
- fi
- done
- if [ restartnum -eq 6 ];then
- echo "重启次数超过了五次"|mail -s "不行啦!" 123@123.com
- exit
- else
- sleep 60
- fi
- fi
- done
这段代码中successsum=0代表成功启动的次数,restartnum代表的启动的次数。看起来很复杂,只要理清思路就没有什么问题了。欢迎探讨。
0
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
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
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
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
- #!/bin/bash
- #****************************************8
- #尝试启动5次
- #5次之后 不成功就发邮件。并退出循环
- function try(){
- a=0
- exec netstat -lnp|grep httpd > /dev/null
- run=`echo $?`
- while [ ! $run -eq 0 ];do
- /etc/init.d/httpd restart 2>/tmp/start_error.log
- netstat -lnp|grep httpd >dev/null
- run=`echo $?`
- a=$[$a+1]
- if [ $a -eq 5 ];then
- a=0
- mail -s httpd_down 996156275@qq.com </tmp/start_error.log
- echo "httpd start fail!!!"
- return 1
- break
- fi
- done
- return 0
- }
- try
- echo "the httpd server exist!!"
- while :;do
- process=$[`ps aux | grep httpd |wc -l`-2]
- if [ $process -ge 500 ];then
- try
- if [ `echo $?` -eq 1];then
- break
- elif [ `echo $?` -eq 0];then
- sleep 60
- process=$[`ps aux | grep httpd |wc -l`-2]
- if [$process -ge 500];then
- echo "process more than 500"|mail -s httpd_process_morethan 500 9961562@qq.com
- exit 0
- fi
- fi
- fi
- sleep 10
- done
- 各路大神看看 有什么问题吗
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
试了试应该没问题。我不会发邮件复制的别人的。
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
这样写能行?
#!/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
#!/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 于 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
初步写出来的,请各位测试并指正!
#!/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
本帖最后由 gxp2008 于 2016-3-16 18:43 编辑
- #!/bin/bash
- while :
- do
- a=`ps -ef |grep httpd |wc -l`
- if [ $a -gt 500 ]
- then
- /etc/init.d/httpd start
- cu=`curl -s -I localhost |grep HTTP|awk '{print $2}'`
- for i in `seq 1 5`
- do
- if [ $cu -ne 200 ]
- then
- /etc/init.d/httpd start
- else
- echo "宕机了"|mail -s "完了" lz_gxp@126.com
- exit
- fi
- done
- else
- sleep 10
- fi
- done
0
[ -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
学习先
[ -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
本帖最后由 阿凯 于 2016-5-3 10:29 编辑
- while : ; do
- m=`ps aux |grep -v 'grep' |grep httpd |wc -l`
- if [ $m -lt 6 ]
- then
- sleep 10
- continue
- else
- /etc/init.d/httpd restart
- n=1
- while [ $? -ne 0 ]
- do
- /etc/init.d/httpd restart
- n=$[$n+1]
- if [ $n > 5 ]
- then
- mail -s "The httpd service start failure" 123@qq.com
- break
- fi
- done
- if [ $? -eq 0 ] ;then
- sleep 60
- if [ $m -lt 6 ] ;then
- sleep 10
- continue
- else
- mail -s "The httpd server is stop,Please check" 123@qq.com
- exit
- fi
- fi
- fi
- done
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
不知道这样写对不对,还请老师指点.
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
不知道这样写对不对,还请老师指点.
编辑回复