# 1.脚本介绍
- 脚本用到lsof命令注意yum一下
yum install -y lsof
- 结合定时任务可每分钟检索运行的nginx,elasticsearch,redis,mysql;如果应用挂掉则尝试重启
# 2.Autorestart.sh
代码语言:javascript复制#!/bin/bash
#log文件
MonitorLog=/home/summer/autostart.log
user=summer
#检测nginx80端口
curtime=$(date " %Y-%m-%d %H:%M:%S")
checkNginx=`echo "summer" | sudo -S lsof -i:80 |grep 'nginx' |grep -v grep |grep -v agent|sort | tail -1 | cut -f 1 -d ' '`
if [[ $checkNginx != 'nginx' ]]; then
echo "$curtime 系统检测到nginx,已挂掉,启动中...." >> $MonitorLog;
/home/$user/nginx/sbin/nginx
if [ $? -eq 0 ]; then
echo "$curtime nginx启动完成" >> $MonitorLog;
else
echo "$curtime nginx启动失败" >> $MonitorLog;
fi
else
echo "$curtime 系统检测到nginx运行正常" >> $MonitorLog;
fi
#检测Redis
#checkRedis=`echo "summer" | sudo -S lsof -i:6379 |grep 'redis-ser' |grep -v grep |grep -v agent|sort | tail -1 | cut -f 1 -d ' '`
checkRedis=`lsof -i:27000 |grep 'redis-ser' |grep -v grep |grep -v agent|sort | tail -1 | cut -f 1 -d ' '`
if [[ $checkRedis != 'redis-ser' ]]; then
echo "$curtime 系统检测到Redis已挂掉,启动中...." >> $MonitorLog;
#单节点6379(注意改lsof端口)
#/home/$user/redis-5.0.8/src/redis-server /etc/redis.conf
#多节点2700(注意改lsof端口)
/home/summer/redis-5.0.8/utils/create-cluster/create-cluster start
if [ $? -eq 0 ]; then
echo "$curtime Redis启动完成" >> $MonitorLog;
else
echo "$curtime Redis启动失败" >> $MonitorLog;
fi
else
echo "$curtime 系统检测到Redis运行正常" >> $MonitorLog;
fi
#检测mysql
checkMysql=`echo "summer" | sudo -S lsof -i:23306 |grep 'mysql' |grep -v grep |grep -v agent|sort | tail -1 | cut -f 1 -d ' '`
if [[ $checkMysql != 'mysqld' ]]; then
echo "$curtime 系统检测到Mysql已挂掉,启动中...." >> $MonitorLog;
mysqld start
if [ $? -eq 0 ]; then
echo "$curtime Mysql启动完成" >> $MonitorLog;
else
echo "$curtime Mysql启动失败" >> $MonitorLog;
fi
else
echo "$curtime 系统检测到Mysql运行正常" >> $MonitorLog;
fi
#检测elasticsearch
ESPID=`ps -ef | grep elasticsearch | grep -w 'elasticsearch' | grep -v 'grep' | awk '{print $2}' | head -n 1` &>/dev/null
if [[ $ESPID ]] ; then
echo "$curtime 系统检测到Elasticsearch运行正常" >> $MonitorLog
else
echo "$curtime 系统检测到Elasticsearch已挂掉,启动中...." >> $MonitorLog
# /home/summer/elasticsearch-7.6.0/bin/elasticsearch -d
/home/$user/elasticsearch-6.3.1/bin/elasticsearch -d
if [ $? -eq 0 ]; then
echo "$curtime Elasticsearch启动完成" >> $MonitorLog;
else
echo "$curtime Elasticsearch启动失败" >> $MonitorLog;
fi
fi
# 3.添加定时任务
代码语言:javascript复制crontab -e
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=""
*/1 * * * * cd /home/summer && ./Autorestart.sh >/dev/null 2>&1