zoukankan      html  css  js  c++  java
  • 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
    

      

  • 相关阅读:
    pycharm使用常见设置
    LeetCode OJ:Insertion Sort List (插入排序链表)
    LeetCode OJ:Reverse Linked List (反转链表)
    Foundations of Qt Development 学习笔记 Part1 Tips1-50
    TCPL学习毕节:第六章hash表
    TCPL学习笔记:4-12以及4-13。关于使用递归的问题。
    几种常见排序算法的C++描述
    一些灵巧的求并算法
    vs中: 错误,未定义的标识符getline 的解决方法
    QT中给程序加上主界面的图标
  • 原文地址:https://www.cnblogs.com/bert227/p/11347817.html
Copyright © 2011-2022 走看看