shell启停服务脚本模板

一、 启动脚本模板:符合幂等性

  如果该服务已经启动,再次调用该脚本,不会报错,也就是说可以反复多次调用,另外启动成功返回 一个参数,提供给自动发布平台校验该服务是否启动

#!/bin/bash
instancename=
# check is instance running
PID=`ps -ef | $instancename | grep -v grep `
if [ ! -z "$PID" ]; then
    echo "instance $instancename is running."
    exit 0
fi

# start instance
# TODO: start cmd


# chenk whether instance be running by url or key word in logfile,choose one or check url
url=
loop=60
count=0
while $count < 60
do
        curl $url && exit 0
        sleep 1
        count=$(($count + 1))
done
if [ $count -ge 60 ];then
        echo "[ERROR]: Timeout ,failed."
        exit 1
fi
echo "[INFO]: Instance $instancename started."

# or check key word in logfile
keyword= xxx
logfile=
orgLineNum=`wc -1 $logfile | cut -d " " -f1`
loop=60
count=0
while $count < 60
do
        endLineNum=`wc -1 $logfile | cut -d " " -f1`
        deltaLine=$(($endLineNum - $orgLineNum))
        tail -n $deltaLine $logfile | sed /$keyword/ && break
        $orgLineNum=$endLineNum
        sleep 1
done
if [ $count -ge 60 ];then
        echo "[ERROR]: Timeout , failed."
        exit 1
fi
echo "[INFO]: Instance $instancename started."  

二、停止脚本,符合幂等性

  可以重复调用

#!/bin/bash
instancename=
#check is instance running
PID=`ps -ef | grep $instancename | grep -v grep `
if [ -z "$PID" ];then
        echo "instance $instancename is not running."
        exit 0
fi

# stop instance
# TODO : stop cmd


# if stop cmd failed ,may kill or exit with error

#or kill
PID=`ps -ef | grep $instancename | grep -v grep `
if [ ! -z "$PID" ];then
        echo "stop cmd failed , try to kill."
        kill $PID
fi

# if kill failed ,may kill -9
if [ ! -z "$PID" ];then
        echo "kill process failed, try to kill -9."
        kill -9 $PID
fi

# or exit with error
PID=`ps -ef | grep $instancename | grep -v grep `
if [ ! -z "$PID" ];then
        echo "stop cmd failed."
        exit 1
fi

  

 

转载于:https://www.cnblogs.com/bert227/p/11347817.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值