Shell脚本管理启动微服务

 · 2018-8-13 · 次阅读


我们都知道有些微服务我们得放后台启动,虽然前者介绍了Supervisord,但是我还是要跟大家讲一讲用Shell去管理微服务,我们一起来看看吧

脚本内容

脚本整体比较简单,我这里就不陈述了,然后实际可用根据自身情况来去衡量,对脚本如有什么建议,可以联系到我,共同讨论

#!/bin/bash
# Descrip : PushClient Service Management Process Script.
# Date    : 2018-08-09 15:29:33
# Author  : beytagh.stark (beytagh.stark@gmail.com)
# Link    : https://www.gov-cn.cn/
# Version : Ver 1.0

PATH=/sbin:/bin:/usr/sbin:/usr/bin

# PushClient launch environment variable
PushHome="/data/servers/PushHome/"
PushClient="/data/servers/PushHome/PushClient.exe"
MonoExec="/usr/bin/mono"
PushName=PushClient.exe
NohupExec="/usr/bin/nohup"


start() {
    echo "Starting PushClient Service."
    $NohupExec $MonoExec $PushClient > /dev/null 2>&1 &
    retval=$?
    if [ $retval -eq 0 ]; then
        echo "Start PushClient Service Success."
    else
        echo "Start PushClient Service Fail."
        pkill -f $PushName
    fi
}

stop() {
    retval=`ps -ef |grep "$PushName" |grep -v "grep" |wc -l`
    if [ $retval -eq 0 ]; then
        echo "PushClient Service Not Running."
    else
        echo "Stopping PushClient Service."
        pkill -f $PushName
        retval=$?
        if [ $retval -eq 0 ]; then
            echo "Stop PushClient Service Success."
        else
            echo "Stop PushClient Service Fail."
        fi
    fi
}

restart() {
    echo "Restarting PushClient Service."
    stop
    start
}

status() {
    retval=`ps -ef |grep "$PushName" |grep -v "grep" |wc -l`
    if [ $retval -eq 0 ]; then
        echo "PushClient Service is Running."
    else
        echo "PushClient Service not Running."
    fi
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    status)
        status
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart|status}"
        ;;
esac