您的位置:首页 > 其它

服务进程自启动脚本,实现每分钟检查服务运行状态

2017-12-04 10:01 483 查看
由于程序真是环境不是由自己运维的,真是负责运维的人员并不了解软件,所以增加一份自启动脚本

脚本原理:利用linux系统的crontab定时任务,每一分钟检查一次系统进程是否存在,若不存在则调用启动命令并记录日志

#!/bin/sh
#启动日志存放路径

startPath=/data/start.log

#检查启动日志是否存在
if [ ! -f $startPath ];then
touch $startPath
chown bfc:bfc $startPath
curDate=`date +%s`
curTime=`date -d @$curDate "+%Y-%m-%d %H:%M:%S"`
echo $curTime: logstart >> $startPath
else
chown bfc:bfc $startPath
fi

#检查mysql是否启动
pid=`ps -ef|grep mysql | grep -v grep |awk '{print $2}'`
if [ "$pid" == "" ];then
service mysql restart
sleep 5
pid=`ps -ef|grep mysql | grep  -v grep | awk '{print $2}'`
if [ "$pid" = "" ];then
curDate=`date +%s`
curTime=`date -d @$curDate "+%Y-%m-%d %H:%M:%S"`
echo "$curTime: mysql start faild!!!" >> $startPath
else
curDate=`date +%s`
curTime=`date -d @$curDate "+%Y-%m-%d %H:%M:%S"`
echo "$curTime: mysql start success." >> $startPath
fi
else
curDate=`date +%s`
curTime=`date -d @$curDate "+%Y-%m-%d %H:%M:%S"`
echo "$curTime: mysql process already exists." >> $startPath
fi

#启动es
pid=`jps -l | grep org.elasticsearch.bootstrap.Elasticsearch | awk '{print $1}'`
if [ "$pid" == "" ];then

su - bfc <<EOF
/data/elasticsearch2017-04-11/elasticsearch-2.4.3/bin/elasticsearch -d;
exit;
EOF

sleep 5
pid=`jps -l | grep org.elasticsearch.bootstrap.Elasticsearch | awk '{print $1}'`
if [ "$pid" == "" ];then
curDate=`date +%s`
curTime=`date -d @$curDate "+%Y-%m-%d %H:%M:%S"`
echo "$curTime: elasticsearch start faild!!!" >> $startPath
else
curDate=`date +%s`
curTime=`date -d @$curDate "+%Y-%m-%d %H:%M:%S"`
echo "$curTime: elasticsearch start success." >> $startPath
fi
else
curDate=`date +%s`
curTime=`date -d @$curDate "+%Y-%m-%d %H:%M:%S"`
echo "$curTime: elasticsearch process already exists." >> $startPath
fi

#启动主服务

pid=`ps -ef | grep tomcat |grep -v grep| awk '{print $2}'`
if [ "$pid" == "" ];then
chmod +x /data/apache-tomcat-8.0.32/bin/*.sh
/data/apache-tomcat-8.0.32/bin/startup.sh
sleep 30
echo "sleep 30"
pid=`ps -ef | grep tomcat |grep -v grep| awk '{print $2}'`
if [ "$pid" == "" ];then
curDate=`date +%s`
curTime=`date -d @$curDate "+%Y-%m-%d %H:%M:%S"`
echo "$curTime: BFC start faild!!!" >> $startPath
else
curDate=`date +%s`
curTime=`date -d @$curDate "+%Y-%m-%d %H:%M:%S"`
echo "$curTime: BFC start success." >> $startPath
fi
else
curDate=`date +%s`
curTime=`date -d @$curDate "+%Y-%m-%d %H:%M:%S"`
echo "$curTime: BFC has started." >> $startPath
fi
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: