#!/bin/bash
http_server() {
port1=$(netstat -ntpl | grep httpd | awk -F ' ' '{print$4}' | cut -d ':' -f 4)
port2=$(cat /etc/httpd/conf/httpd.conf | grep Listen | grep -v '#' | awk '{print$2}')
if [ $port1 = $port2 ]
then
echo "1"
else
echo "0"
fi
}
for i in `seq 100`
do
http_server
sleep 3
if [ $(http_server) = 1 ]
then
echo [$(date +%F%n%T)] "服务正常运行" >> /opt/http_server.log
else
echo [$(date +%F%n%T)] "http服务出现错误,正在重启" >> /opt/http_server.log
systemctl start httpd
fi
HOME1=$(cat /etc/httpd/conf/httpd.conf | grep DocumentRoot | grep -v "#" | awk '{print$2}' | cut -d '"' -f 2)
if [ -z $(ls $HOME1)]
then
echo [$(date +%F%n%T)] "http根目录文件不存在,正在尝试添加文件" >> /opt/http_server.log
cp /opt/grzy.zip /var/www/html/
unzip -d /var/www/html/ /var/www/html/grzy.zip
sleep 4
echo [$(date +%F%n%T)] "添加完成,重新启动" >> /opt/http_server.log
systemctl restart httpd
else
echo [$(date +%F%n%T)] "服务正常运行" >> /opt/http_server.log
fi
done
在原有的基础上添加了日志,日期
检测http服务根目录文件是否存在,不存在则自动添加并重新启动
简单的http监控服务