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

由于程序真是环境不是由自己运维的,真是负责运维的人员并不了解软件,所以增加一份自启动脚本

脚本原理:利用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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值