由于服务器长期受到黑客攻击,导致httpd服务经常掉线,为保证服务正常运行,特找了一些方法来实现httpd服务自启。

在windows中有dos命令,在linux中有shell命令,这里我们服务器的操作系统是Linux,所以采用的是shell脚本。
首先使用vim工具创建一个文件来存储shell命令

vim autorestart

在第一行插入

#!/bin/bash

让其将这个文件识别为一个可执行脚本
然后使用

URL="http://127.0.0.1/"
curlit()
{
curl --connect-timeout 15 --max-time 20 --head --silent "$URL" | grep '200'
}
doit()
{
if ! curlit; then
systemctl restart httpd.service
fi
}
while true; do
doit > /dev/null
#systemctl reload httpd.service
sleep 30
done