1、编辑脚本文件netstart.sh
vim netstart.sh
#!/bin/bash
# 网站url地址
URL="https://www.***.com/"
mdate=`date +%R-%d-%m-%Y`
# log日志
LogFile=/home/logs/net.log
# 获取http响应代码
HTTP_CODE=`curl -o /dev/null -s -w "%{http_code}" "${URL}"`
# echo $HTTP_CODE
# 服务器能正常响应,应该返回200的代码
if [ $HTTP_CODE -ne 200 ];then
# 重启服务
# apache 服务
# systemctl restart httpd.service
# nginx 服务
nginx -s reload
echo Restart the success ! $mdate > $LogFile
fi
2、添加定时任务
[root@localhost /]# crontab -e
在最下方增加一条
# 每分钟检查1次
*/1 * * * * /home/sh/netstat.sh
保存退出即可