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

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

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

有一台服务器运行着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服务崩溃自动重启脚本(监控进程服务并自动重启进程服务)脚本” 的相关文章

php高效检测远程图片是否存在

php高效检测远程图片是否存在function img_exits($url){     $ch = curl_init();    &…

微软版Ghost Win10:FFU映像备份和还原

微软版Ghost Win10:FFU映像备份和还原

在日常的维护中,系统的备份和还原是大家经常需要操作的事情。虽然Windows 10已经提供很多的工具,如系统还原、WIM备份/还原,VHD备份等。不过这些工具大多是基于文件的备份/还原。我们以前经常的使用的Ghost则是基于扇区的备份/还原…

centos 配置Let's Encrypt 泛域名https证书

centos 配置Let's Encrypt 泛域名https证书

前言2018年1月份Letsencrypt可以申请泛域名证书,这让我们部署多域名、多站点https省了很多功夫,终于可以不用维护多个域名的https证书。笔者以acme.sh为例,手把手教你配置https证书~本教程适用于centos 6.…

PHP和Redis实现在高并发下的抢购及秒杀功能

抢购、秒杀是平常很常见的场景,面试的时候面试官也经常会问到,比如问你淘宝中的抢购秒杀是怎么实现的等等。抢购、秒杀实现很简单,但是有些问题需要解决,主要针对两个问题:一、高并发对数据库产生的压力二、竞争状态下如何解决库存的正确减少("…

抢先体验太阳谷!20H1、20H2、21H1替换“Dev版新图标”

尽量不要替换shell32.dll.mun和zipfldr.dll.mun,这两老哥可能会带来无法预测的风险!不解除被替换文件的硬链接,已确定会导致无法安装质量更新!(详见H大测评)单替换imageres.dll.mun新图标覆盖率基本可达…

阿里云盘 分享脚本 抢先实现文件分享功能

Win端基本所有浏览器均可进行安装安装地址:https://bbs.tampermonkey.net.cn/thread-427-1-1.html如何安装:Tampermoneky安装:谷歌浏览器参考:https://bbs.tamperm…

发表评论

访客

看不清,换一张

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