监测服务器 端口&进程&文件
定时任务
crontab -e
*/5 * * * * /bin/bash /root/http.sh
检测服务器端口是否开放
#!/bin/bash
nc -w 10 127.0.0.1 -z 80 > /dev/null 2>&1
if [ $? -eq 0 ]
then
echo "http is ok"
else
echo "[`date +%F/%H:%M:%S`]http is stop" >> /root/http.log
service httpd restart
fi
检测服务器进程是否存在
#!/bin/bash
ps -ef|grep httpd |grep -v grep > /dev/null 2>&1
if [ $? -ne 0 ]
then
echo "[`date +%F/%H:%M:%S`]http is stop" >> /root/http.log
service httpd start
else
echo "http is ok"
fi
自循环检查文件是否存在
#!/bin/bash
while true;do
today=`date -d yesterday +%Y%m%d`
file="apache_$today.tar.gz"
if [ -f "$file" ]
then
echo "file is ok"
else
echo "error $file" > /root/file.log
mail -s "fail backup from test" admin@server.com </root/file.log
fi
sleep 300
done