shell 管理SpringBoot 生产环境服务 2

前面有篇博客说如果使用shell 命令管理SpringBoot 程序,后面在业务的使用上发现用那种方式不合适所有程序的关闭和重启。业务前面是使用 命名找到对应程序运行的pid 然后直接kill 掉。

上一篇博客地址:http://blog.csdn.net/jiangzeyin_/article/details/74671334

这样的情况如果对应程序中存在有异步执行或者有线程池还在运行状态。那么kill 会直接挂掉程序。那么这样程序的运行数据就会丢失。这样是我们很不想看到的结果。最后想到增强版解决这个问题

直接上代码

# description: Auto-starts boot

Tag="AppApiApplication"
MainClass="com.yoke.AppApiApplication"
Lib="/test/api/lib/"
Log="/test/api/run.log"
Port="5000"
Token=
echo $Tag
RETVAL="0"

# See how we were called.
function start() {
    echo  $Log 
    if [ ! -f $Log ]; then
        touch $Log
    fi
    nohup java -Dappliction=$Tag -Djava.ext.dirs=$Lib":${JAVA_HOME}/jre/lib/ext" $MainClass > $Log 2>&1 &  
    tailf $Log
}


function stop() {
    pid=$(ps -ef | grep -v 'grep' | egrep $Tag| awk '{printf $2 " "}')
    if [ "$pid" != "" ]; then      
        echo -n "boot ( pid $pid) is running" 
        echo
        echo -n "stop web boot"
        wget http://127.0.0.1:$Port/sys/shutdown?token=$Token
        echo
        echo -n $"Shutting down boot: "
        pid=$(ps -ef | grep -v 'grep' | egrep $Tag| awk '{printf $2 " "}')
        if [ "$pid" != "" ]; then
            echo "kill boot process"
            kill -9 "$pid"
        fi
        else 
             echo "boot is stopped" 
        fi

    status
}

function status()
{
    pid=$(ps -ef | grep -v 'grep' | egrep $Tag| awk '{printf $2 " "}')
    #echo "$pid"
    if [ "$pid" != "" ]; then
        echo "boot is running,pid is $pid"
    else
        echo "boot is stopped"
    fi
}



function usage()
{
   echo "Usage: $0 {start|stop|restart|status}"
   RETVAL="2"
}

# See how we were called.
RETVAL="0"
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    reload)
        RETVAL="3"
        ;;
    status)
        status
        ;;
    *)
      usage
      ;;
esac

exit $RETVAL

这里大致解释一下
这种方式是先请求预先预留的接口 (访问这个接口表示要关闭程序,然后重新里面作出相应的释放资源操作,然后在kill 掉)

这里面比上一次的命令新增倆个变量
Port 表示运行程序对应端口
Token 辅助变量 表示对应接口的token (token 这里就不解释如何用了,如果理解的自行脑补)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值