当前位置:首页 > 技术分享

Nginx服务崩溃自动重启脚本(监控进程服务并自动重启进程服务)脚本

admin5年前 (2021-04-01)技术分享4723

有一台服务器运行着Ngin最近突然有一次崩溃,导致使用方当天无法访问网页端,然后我不得不登录服务器,检查各项服务,发现nginx崩溃了,于是重启Nginx,问题解决。

后来为了防止Nginx再发生这种情况给运维带来的运维成本,于是写了一个脚本,放到Linux计划任务里进行监听管理Nginx服务,一旦崩溃,那么就自动重启Nginx服务。当然该脚本适合各种后台服务,可以简单修改,适应各种后台服务程序。

首先编写shell脚本service-restart.sh

方法一

#! /bin/bash
# service-restart.sh
# description: 这个程序用来监控服务器服务软件,以程序挂掉后重启 Author is xiaogg
# ! /bin/sh

phpfpm_procnum=`ps -ef|grep "php-fpm"|grep -v grep|wc -l`
nginx_procnum=`ps -ef|grep "nginx"|grep -v grep|wc -l`
mysql_procnum=`ps -ef|grep "mysqld"|grep -v grep|wc -l`
redis_procnum=`ps -ef|grep "redis"|grep -v grep|wc -l`

if [ $phpfpm_procnum -eq 0 ]
then
	echo "start php-fpm..."
	/etc/init.d/php-fpm start >> /var/log/listen_service_start.log 2>&1
elif [ $nginx_procnum -eq 0 ]
then 
	echo "start nginx..."
	/usr/local/nginx/sbin/nginx >> /var/log/listen_service_start.log 2>&1
elif [ $mysql_procnum -eq 0 ]	
then 
    echo "start mysql..."
    systemctl start mysqld >> /var/log/listen_service_start.log 2>&1
elif [ $redis_procnum -eq 0 ]
then 
    echo "start redis..."
    systemctl start redis >> /var/log/listen_service_start.log 2>&1
else 
	#echo "no cmd"
	exit
fi


其实主要内容就是

检查监听服务是否还正常

对运行不正常的服务进行重启

主要写了 php-fpm nginx mysqld redis 如果不需要就把相关行删除或注释

方法二

#! /bin/bash
# service-restart.sh
# description: 这个程序用来监控端口是否通畅,不通后重启 Author is xiaogg
# ! /bin/sh

if [ $(id -u) != "0" ]
then
        echo "Not the root user! Try using sudo command!"
        exit 1
fi

netstat -anop | grep 0.0.0.0:80
if [ $? -ne 1 ]
then
        exit
fi
 
echo $(date +%T%n%F)" Restart nginx Services " >> service_start.log
#/usr/local/nginx/sbin/nginx
/etc/init.d/php-fpm restart
/bin/systemctl restart nginx.service

说明该方法主要是监控端口还判断是否通畅.

第一步先验证是否是root  这一步完全可以省略

第二步会重启并添加日志


方法三

#! /bin/bash
# service-restart.sh
# description: 这个程序用来监控网址是否返回200状态,不是后重启 Author is xiaogg
# ! /bin/sh

url=http://blog.bitefu.net

check_http(){
  status_http=$(curl -m 5 -s -I $url | grep HTTP | awk '{print $2}')
}
check_http
if [ "$status_http" != "200" ]
then
    echo $(date +%T%n%F)" Restart nginx Services " >> service_start.log
	#/usr/local/nginx/sbin/nginx
	/etc/init.d/php-fpm restart
	/bin/systemctl restart nginx.service
else
    #echo "no cmd"
	exit
fi

方法三原理说明

该方法主要是监控域名状态码 是不是200 如果不是就去重启服务


几种方法侧重点不一样可以结合到一起使用


加入Linux crontab自动任务里即可:

crontab -e 
*/1 * * * * sh /root/service-restart.sh

第一行是编辑计划任务

第二行添进去保存就行了可以通过crontab -l查看是否添加成功

第二行添加的是每分钟执行



扫描二维码推送至手机访问。

版权声明:本文由小刚刚技术博客发布,如需转载请注明出处。

本文链接:https://blog.bitefu.net/post/154.html

分享给朋友:

“Nginx服务崩溃自动重启脚本(监控进程服务并自动重启进程服务)脚本” 的相关文章

在CentOS 5.x 6.x里使用yum源 换阿里云国内源换vault.centos.org源

阿里云CentOS 5 的系统,无法用yum来安装应用软件。  原因:CentOS 5 在2017-03-31日已经结束支持,不再提供维护更新,所以包括阿里云镜像站的文件可能都是过时或已经有部分文件缺失。 &n…

安卓模拟器连接端口 及常用命令

下面是我总结和测试通过的:有的是搜集来的模拟器名称                     &nbs…

Chrome 最全历史版本下载以及chromedriver下载

https://vikyd.github.io/download-chromium-history-version/#/ https://dl.lancdn.com/landian/soft/chrome/m/ chromedriver的版…

python 高速处理分析超大网站日志文件 带进度条手动输入日志文件

 python 高速处理分析超大网站日志文件 带进度条手动输入日志文件    1 统计本日志文件的总pv、uv    2 列出全天每小时的pv、uv数    3 列出to…

记一次阿里云服务器cc攻击防护 windows 2012 iis8

记一次阿里云服务器cc攻击防护 windows 2012 iis8

上次连续一周左右阿里云服务器都在遭受cc攻击.导致访问量特别大,节假日接口调用特别缓慢或者根本访问不了的情况.本身服务器安装了 网站安全狗(IIS版) .并开始了防cc攻击.但是呢,平时还行,这次压力山大.于是一气之下用pytho…

全世界最快dns分享 Public DNS Server List

偶然发现一个共享全世界最快dns的网站,当然也包含中国了https://public-dns.info/ 中国https://public-dns.info/nameserver/cn.html…

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。