publish.sh 2.14 KB
#!/usr/bin/env bash

pid= #进程pid
# 启动jar包的名字
packageName=docking-api-0.0.1-SNAPSHOT.jar

counter=0

max_attempts=10

#检测pid
function getPid()
{
    echo "检测状态---------------------------------------------"
    pid=$(ps -ef | grep -n ${packageName} | grep -v grep | awk '{print $2}')
    if [ -n "${pid}" ]
    then
        echo "运行pid:${pid}"
    else
        echo "未运行"
    fi
}

#停止程序
function stop()
{
    getPid
    if [ -n "${pid}" ]
    then
        echo "停止程序---------------------------------------------"
        kill -15 ${pid}
        while [ $counter -lt $max_attempts ]; do
             getPid
             if [ ${pid} ]
             then
                 #stop
                 sleep 1
             else
                 echo "停止成功,尝试${counter}次"
                 break
             fi
            ((counter++))
        done
        getPid
        if [ ${pid} ]
        then
            #stop
            echo "停止失败"
        else
            echo "停止完成"
        fi
    fi
}



#启动程序
function start()
{
    #启动前,先停止之前的
    stop
    if [ -n "${pid}" ]
    then
        echo "停止程序失败,无法启动"
    else
        echo "启动程序---------------------------------------------"
        # 删除out输出文件
        rm -rf ./console.out
        nohup java   -Xms500m -Xmx800m -jar   ./$packageName  --spring.profiles.active=prod  >./console.out 2>&1 &
        #查询是否有启动进程
        getPid
        if [ ${pid} ]
        then
            echo "已启动"
            #nohup日志
            tail -n 50 -f ./console.out
        else
            echo "启动失败"
        fi
    fi
}

#启动时带参数,根据参数执行
if [ ${#} -ge 1 ] 
then
    case ${1} in
        "start") 
            start
        ;;
        "restart") 
            start
        ;;
        "stop") 
            stop
        ;;
        *) 
            echo "${1}无任何操作"
        ;;
    esac
else
    echo "
    command如下命令:
    start:启动
    stop:停止进程
    restart:重启
    示例命令如:./publish start
    "
fi