绿皮驿站

分享
简单的在Linux中启动关闭进程

1. 在项目的路径下创建名为boot.conf的空白文件,并将以下属性写入文件,设置好参数 

2. 在/bin路径下,创建名为boot的空白文件,并将以下脚本写入文件,即可在全局使用boot命令  

3. 在项目的路径下执行boot start命令即可启动项目,如需查看其它命令请输入boot help 


boot.conf

RUN_CMD="java -jar xxx.jar" 
RUN_PID="run.pid" 
RUN_LOG="run.log"


boot

#! /bin/bash

#boot.conf

#RUN_CMD="java -jar xxx.jar"
#RUN_PID="run.pid"
#RUN_LOG="run.log"

BOOT_CONF="boot.conf"

OPTION=$1

linkConf () {
    if [ -f "$BOOT_CONF" ]
    then
        source ./$BOOT_CONF
    else
        echo not found $BOOT_CONF
        exit
    fi
}

start () {
    status
    STATUS=$?
    if [ "$STATUS" == 0 ]
    then
        nohup $RUN_CMD > $RUN_LOG &
        echo $! > $RUN_PID
        sleep 1
        return 1
    fi
    return 0
}

stop () {
    status
    STATUS=$?
    if [ "$STATUS" == 1 ]
    then
        kill `cat $RUN_PID`
        while :
        do
            status
            STATUS=$?
            if [ "$STATUS" == 0 ]
            then
                return 1
            fi
            sleep 1
        done
    fi
    return 0
}

status () {
    if [ -f "$RUN_PID" ]
    then
        PID=`cat $RUN_PID`
        RESULT=`ps -p $PID|grep -v PID`
        if [ "$RESULT" != "" ] 
        then
            return 1
        fi
    fi
    return 0
}

help () {
    echo usage: boot [start] [stop] [status] [restart]
}

if [ "$OPTION" == "start" ] 
then
    linkConf
    echo starting ...
    start
    RESULT=$?
    if [ $RESULT == 1 ]
    then
        echo start success
    else
        echo start already
    fi
elif [ "$OPTION" == "stop" ] 
then
    linkConf
    echo stopping ...
    stop
    RESULT=$?
    if [ $RESULT == 1 ]
    then
        echo stop success
    else
        echo stop already
    fi
elif [ "$OPTION" == "status" ] 
then
    linkConf
    status
    RESULT=$?
    if [ $RESULT == 1 ]
    then
        echo running
    else
        echo shutdown
    fi
elif [ "$OPTION" == "restart" ] 
then
    linkConf
    echo restarting ...
    echo check status
    status
    RESULT=$?
    if [ $RESULT == 1 ]
    then
        echo running
        echo stopping ...
        stop
        RESULT=$?
        if [ $RESULT == 1 ]
        then
            echo stop success
        else
            echo stop already
        fi
    else
        echo shutdown
    fi
    echo starting ...
    start
    RESULT=$?
    if [ $RESULT == 1 ]
    then
        echo start success
    else
        echo start already
    fi
else
  help
fi


发布于 2020年9月11日   浏览(106)