加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

shell启停服务脚本模板

发布时间:2020-12-16 01:44:16 所属栏目:安全 来源:网络整理
导读:一、 启动脚本模板:符合幂等性 如果该服务已经启动,再次调用该脚本,不会报错,也就是说可以反复多次调用,另外启动成功返回 一个参数,提供给自动发布平台校验该服务是否启动 #!/bin/bashinstancename=# check is instance runningPID=`ps -ef | $instanc

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

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

#!/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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读