mongodb监控并在服务挂掉后自动重启脚本

今天遇到一个情况,一台用来数据可视化的mongodb服务器中的mongodb服务在进行数据抽取的情况经常出现挂掉的情况,所以写了一个小脚本。监控mongodb服务是否启动,如果服务挂掉后启动自动重启服务.

此脚本也适用于其他服务。如:nginx,mysql,redis等

服务启动脚本代码如下

#!/bin/bash
#检查是否是root用户

if [ $(id -u) != "0" ]
then
echo "Not the root user! Try using sudo command!"
exit 1
fi
#监控服务是是否存活,这里是通过监控端口来监控服务,这里也可以替换为其他服务
netstat -anop | grep 127.0.0.1:27017
if [ $? -ne 1 ]
then
exit
fi

echo $(date +%T%n%F)" Restart mongodb Services " >> mongodb.log
#重启服务
/script/mongodb-27017.sh restart

  


将脚本加入计划任务。每1min检测一次

crontab -e

*/1 * * * * sh /sh/mongodb.sh

猜你喜欢

转载自www.cnblogs.com/adjk/p/12145426.html