{{{密码回复可见}}}
while :
do
port=`netstat -lnp |grep :80`
if [ -z "$port" ]
then
echo "http down" | mail -s "httpd problem" test@11.com
/usr/local/apache2/bin/apachectl restart
sleep 30
else
exit 0
fi
done
#!/bin/bash
while :
do
n=`netstat -lnp|grep 80|wc -l`
if [ n -gt 0 ]
then
continue
else
service httpd restart
echo 'restat httpd' |mail -s 'xx' xx@163.com
fi
sleep 30s
done
echo "test.sh" >> /var/spool/cron/root
if netstat -lnp |grep -Eq ':80' #一定要精确匹配否则误判 -q 安静
then
exit #有返回值为真0
else
service httpd restart >/dev/null 2>&1
echo '80 port is down.' |mail -s 'check_80' xx@163.com
n=`ps aux |grep httpd|grep -cv grep`
if [ $n -eq 0 ]; then
/usr/local/apache2/bin/apachectl start 2>/tmp/apache_start.err
fi
if [ -s /tmp/apache_start.err ]; then
cat /tmp/apache_start.err |mail -s 'apache_start_error' $mail
fi
fi
while [ 1 ]
do
n=$[`netstat -lnp |grep ':80' |wc -l`]
if [ $n == 1 ]
then
echo "the httpd service is OK"
else
/etc/init.d/httpd start
echo "the httpd service is down" |mail -s 15779025129@163.com
fi
sleep 30
done
- a=`netstat -lnp |grep 80 |awk '{print $4}' |sed 's/\::://'`
- if [ -z $a ]
- then
- service httpd start
- echo "http is down" | mail -s httpd is down lz_gxp@126.com
- else
- echo "It's OK"
- fi
a=`netstat -lnpt |grep 80 |awk -F / '{print $2}'`
if [ -z $a ] ;then
service nginx start
echo "httpd restart" |mail -s "waring!! httpd " zyos@qq.com
else
echo ok > /dev/null
fi
#!/bin/bash
while :;do
a=`netstat -lnp|grep :80|awk '{print $4}'|awk -F ':' '{print $2}'`
if [ -z $a ]
then
service httpd restart
mail -s "service nginx restart" jason@qq.com
fi
sleep 30
done
- [root@cacti-client ~]# cat checkservice.sh
- #!/bin/bash
- http=`netstat -lnp |grep 80`
- while :;
- do
- if [ -z $http ]
- then
- echo "httpd is down" | service httpd restart | mail -s "oo" 280472479@qq.com
- else
- echo "httpd is ok"
- fi
- sleep 30
- done
- /code]
- <div class="blockcode"><blockquote>crontabe -e:
- */1 * * * * /bin/bash /root/checkservice.sh
if netstat -lnp|awk '{print $4}'|grep -q ':333$'; then exit; else serive httpd start;echo "邮件内容" |mail -s "主题" abc@139.com; sleep 30; fi
#!/bin/bash
#Written by dwt at 2016-05-27
#判断本机的80端口是否开启着,如果开启着什么都不做,如果发现端口不存在,那么重启一下httpd服务,并发邮件通知你自己。脚本写好后,可以每一分钟执行一次,也可以写一个死循环的脚本,30s检测一次。
port=`netstat -lnp|grep 25|awk '{print $4}'|awk -F ':' '{print $2}'|grep -v '^$'`
while :
do
if [ -z $port ]
then
echo "Httpd is already started"
else
echo "The 80 Port is stop "
mail -s "Httpd is stopped" abc@126.com
/etc/init.d/start
fi
sleep 30done
ip=`ifconfig |head -2|tail -1|awk '{print $2}'|awk -F ':' '{print $2}'`
while ! netstat -lnp|grep 80
do
/etc/init.d/httpd restart
echo "Http of $ip is down" |mail -s "Warnging" abc@139.com
sleep 30
done
#!/bin/bash
while :
do
if [ -z $(netstat -pantu|grep 80) ]
then
/etc/init.d/httpd start
fi
sleep 30
done
while [ -z `netstat -lnp |grep '0.0.0.0:80'` ]
do
/etc/init.d/nginx restart
mail -s 'nginx restart' 123@qq.com 'nginx restart'
sleep 60
done
##
##
a=":::80"
b=`netstat -lnp|grep httpd |awk -F' ' '{print $4}'`
while :;do
b=`netstat -lnp|grep httpd |awk -F' ' '{print $4}'`
if [ $b == $a ];
then
echo "The httpd service is ok!"
else
service httpd restart
echo "The httpd service is down!" |mail -s "主题" spacehuman@163.com
echo "The httpd service was started!"
fi
sleep 30
done
netstat -ntulp |grep 80
a=$?
if [ ! $a -eq 0 ];then
service httpd start
echo "httpd service error, restart"
fi
port=`netstat -lnp|grep ":80"|wc|awk -F " " '{print $1}'`
while :; do
if [ $port -ge 1 ]
then exit
else /etc/init.d/httpd restart
echo echo "the 80 port has been down now restart it!"|mail -s "restart the 80 port" chenconghe@benco.com.cn
fi
sleep 30
done
~
#! /bin/bash
#
port=/tmp/port
[ -d /tmp/port ] || mkdir /tmp/port
while :
do
netstat -lnp|awk '{print $4}'|awk -F':' '{print $NF}' | grep -v '^$' |grep '^[0-9]' > /tmp/port/port.txt
if grep -q '80' /tmp/port/port.txt
then
echo "ok, httpd 服务已经开启。"
else
echo "httpd 服务没有开启,现在开启服务。"
/etc/init.d/httpd start
echo "now start httpd"| mail -s "notice" 15769162764@163.com
fi
sleep 30
done
#我的思路,首先,通过找到 端口号,把端口号,放到port.txt 中,之后 用grep -q 去判
断 有没有80 端口 ,如果有的话,输出服务开启,如果没有,就发邮件,重启服务。##
##
##
while :
do
if netstat -lnp | grep 80| grep -q httpd
then
echo "httpd is start"
else
if netstat -lnp | grep 80 | grep -q nginx
then
/etc/init.d/nginx stop > /dev/null
fi
/etc/init.d/httpd start
echo "httpd start...."
fi
sleep 30
done
!/bin/bash
判断本机的80端口是否开启着,如果开启着什么都不做,如果发现端口不存在,那么重启一下httpd服务
,并发邮件通知你自己。脚本写好后,可以每一分钟执行一次,也可以写一个死循环的脚本,30s检测一>
次。#每分钟执行一次就去使用crond服务:crontab -e 定义 * 命令行
这里写一个死循环,30分钟检测一次,为了测试加两条报信的,来个反应
while :;do
netstat -lnp |grep :80 > /dev/null
if [ $? -eq 0 ];then
echo "80 is up"
else
echo "80 is down"
/etc/init.d/httpd restart > /dev/null
fi
sleep 30
done
忘记发一下邮件了
EvilAnne - m
#!/bin/bash
#写一个脚本,判断本机的80端口是否开启着,如果开启着什么都不做,如果发现端口不存在,那么重启一下httpd服务,并发邮件通知你自己。脚本写好后,可>以每一分钟执行一次,也可以写一个死循环的脚本,30s检测一次。
mail=123@qq.com
while :;do
apacheport=`netstat -tnl|grep 80|awk '{print $4}'|cut -c 4-`
if [ $apacheport == "0" ];then
apachestart=`service httpd restart`
echo "start...king $apachestart"|mail -s "message" $mail
fi
sleep 60
done
#!/bin/bash
a=`netstat -lnp |grep '80'|grep -v ':::80' |awk '{print $1}'`
b=`rpm -qa |grep 'sendmail'`
if [ -z "$b" ]
then
yum install -y sendmail
service sendmail start
else
service sendmail restart
fi
while :
do
if netstat -lnp |grep ':80' |grep -q 'LISTEN'
then
echo "the 80 port is alive" >/dev/null
else
/usr/local/apache4/bin/apachectl restart >/dev/null 2>&1
echo "the 80 port is down," |/usr/sbin/sendmail -s "please check_80" 327357283@qq.com
n=`ps aux |grep httpd|grep -cv grep`
if [ $n -eq 0 ]
then
/usr/local/apache4/bin/apachectl start 2>> /usr/local/apache4/logs/error_log
echo `tail /usr/local/apache4/logs/error_log`|/usr/sbin/sendmail -s "please check error_log" 327357283@qq.com
fi
fi
sleep 60
done
看了老师的答案才知道怎么写,把脚本放到后台执行就可以了 nohup &
#Description: This script is to check if httpd service is alive or not
#Author: Jiazhi Yang
#Date: 14/11/2016
#Script Name: checkhttpd.sh
service=`netstat -lnp |grep httpd |awk '{print $7}' |awk -F'/' '{print $2}'`
if [ "$service" == "httpd" ]; then
echo "Service httpd is alive!"
else
/etc/init.d/httpd restart |mail -s "Service httpd restarted!!" xxxx@139.com
echo "httpd restarting finished!"
fi
*/1 * * * * /bin/bash /data/yangjz/exercise/checkhttpd.sh
#!/bin/bashwhile :;do a=`netstat -lnp |grep ":80"` if [ -z $a ];then echo "port80 is down" | mail -s port80 is down XXX@qq.com /etc/init.d/httpd restart fi sleep 30done
#!/bin/bash
a=`netstat -plnp |grep 8080 |grep -v grep |wc -l`
echo $a
if [ $a -eq 0 ]
then
/etc/init.d/httpd start
if [ $? -eq 0 ]
then
echo "succefully" |mail -s '111' 1021887808@qq.com
fi
else
echo "OK"
fi
while :
do
/bin/netstat -lnp |grep :80 > /dev/null
if [ $? != 0 ]
then
/usr/local/apache2/bin/apachectl restart >/dev/null
echo "Restart Apache"
fi
sleep 30
done
~
编辑回复