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
    

      

  • 相关阅读:
    .NET实现Excel文件的读写 未测试
    权限管理设计
    struts1中配置应用
    POJ 2139 Six Degrees of Cowvin Bacon(floyd)
    POJ 1751 Highways
    POJ 1698 Alice's Chance
    POJ 1018 Communication System
    POJ 1050 To the Max
    POJ 1002 4873279
    POJ 3084 Panic Room
  • 原文地址:https://www.cnblogs.com/bert227/p/11347817.html
Copyright © 2011-2022 走看看