控制Linux下 mono 服务的启动停止

当Window下的服务部署到Linux的时候,我们一般用Mono.service 来启动停止。参数比较多,不太好用。于是有个这个Shell脚本。

用法:moa s1 start #启动  

        moa s1 stop # 停止

 

要求有如下的目录结构:

/opt/basepath/

                    s1/

                         sample1.exe #服务1,文件名称不限

                    s2/

                         sampleservice2.exe #服务2 ,文件名称不限

 

                    daemon.d/  #空目录,放置进程标识

                    moa  #Shell脚本

 

#!/bin/bash
# 
# monosd  执行Mono.Service 启动停止 .net 服务,根据目录名.
# 例如:目录结构如下时候, monosd s1 start/stop
# /opt/basedir/
# /opt/basedir/s1
# /opt/basedir/s1/xxxxx.exe
# 
#   
# chkconfig: - 50 50
# description: start or stop mono service by pathname
# config: /etc/sysconfig/netconsole
#
# Copyright 2015 Qtong, Inc.
#
# Based in part on a shell script by
# evlon <evlion@qq.com> 2015-11-02

MONOPATH=/opt/mono/bin


SHNAME=$0
SERVICENAME=$1
#BASEPATH=/opt/${SHNAME##*/}
#BASEPATH=${SHNAME%/*}
BASEPATH=$(cd `dirname $0`; pwd)
DAEMONPATH=$BASEPATH/daemon.d

PATH=$MONOPATH:$PATH
RETVAL=0



EXEFILE=`find $BASEPATH/$SERVICENAME/ -name *.exe`
EXEFILE=${EXEFILE##*/}


usage ()
{
    echo $"Usage: $0 servicePathName {start|stop}" 1>&2
    RETVAL=2
}


start ()
{
    if [ ! -d $DAEMONPATH ]; then 
        echo $"目录不存在 $DAEMONPATH" 1>&2
        exit 3
    fi
    if [ ! -d "$BASEPATH/$SERVICENAME" ]; then 
        echo $"目录不存在 $BASEPATH/$SERVICENAME" 1>&2
        exit 4
    fi

    
    
    if  [ "$EXEFILE" == "" ]; then 
        echo $"找不到可执行文件!目录: $BASEPATH/$SERVICENAME/" 1>&2
        exit 5
    fi
            mono-service -d:$BASEPATH/${SERVICENAME} -l:$DAEMONPATH/${SERVICENAME}d $EXEFILE 
    sleep 3
    
    if [ ! -f  $DAEMONPATH/${SERVICENAME}d ]; then
        echo "PID:"`cat $DAEMONPATH/${SERVICENAME}d` "Started"  1>&2
    fi
}

stop ()
{
    if [ ! -d $BASEPATH/$SERVICENAME ]; then 
        echo $"目录不存在 $BASEPATH/$SERVICENAME" 1>&2
    
        exit 3
    fi

    if [ ! -f  $DAEMONPATH/${SERVICENAME}d ]; then 
        echo $"服务没有运行 $BASEPATH/$SERVICENAME" 1>&2
        
        exit 5
    fi
    PID=`cat $DAEMONPATH/${SERVICENAME}d`
    kill `cat $DAEMONPATH/${SERVICENAME}d` 
    echo "PID: $PID Stoped"  1>&2
}




restart ()
{
    stop
    start
}


case "$2" in
    stop) stop ;;
    start) start;;
    restart|reload|force-reload) restart ;;
     *) usage ;;
esac

exit $RETVAL
posted @ 2015-11-03 20:39  阿牛  阅读(2611)  评论(0编辑  收藏  举报