欢迎访问自建博客原文:
http://www.e-lionel.com/index.php/2019/12/05/112/
安装crontab:
yum install -y vixie-cron crontabs
脚本:检查nginx状态,如果nginx没有运行就重启
vim /home/check_nginx.sh
function log(){
echo "[`date +"%Y-%m-%d %H:%M:%S"`] $1"
}
status=`/sbin/service nginx status | sed -n '1p;1q'`
log "Nginx status is : $status"
if [[ $status == *running* ]]
then
log 'Nginx is OK.'
else
log 'Nginx is Die.'
log 'nginx -s stop'
/usr/sbin/nginx -s stop
log 'ps -aux | grep nginx | grep -v grep | grep process | nl | awk {print $3} | xargs kill -9'
ps -aux | grep nginx | grep -v grep | grep process | nl | awk '{print $3}' | xargs kill -9
log 'nginx -c /etc/nginx/nginx.conf'
/usr/sbin/nginx -c /etc/nginx/nginx.conf
fi
log '----------------------------------------------------------------------------------'
授予执行权限:
chmod +x /home/check_nginx.sh
创建定时任务:
crontab -e
#检测nginx状态,每10秒检查一次。centos上crontab最小间隔是1分钟
/bin/sh /home/check_nginx.sh >> /home/check_nginx.log
创建日志文件:
touch /home/check_nginx.log
使定时任务生效:
service crond restart
来源:CSDN
作者:lionel0724
链接:https://blog.csdn.net/lionel0724/article/details/104036618