springboot执行linux脚本,基于 Shell 脚本启动、停止 SpringBoot 应用

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/myNameIssls/article/details/84841567

文章目录

基于 Shell 脚本启动、停止 SpringBoot 应用

需求描述

场景分析

实现方案

创建脚本文件

授权

使用方式

测试

基于 Shell 脚本启动、停止 SpringBoot 应用

需求描述

常规的SpringBoot应用是通过java -jar *.jar方式来启动的,为了更好的管理SpringBoot应用的生命周期,可以通过使用Shell脚本来实现对其生命周期的控制。

场景分析

通过Shell脚本直接启动、关停、重启SpringBoot应用。

示例:

|—— /

|—— bin

|—— boot

|—— application

|—— springboot-example-0.0.1-SNAPSHOT.jar

bin目录放置boot脚本,application目录放置SpringBoot应用。

通过boot [APP_NAME] [start|stop|restart|status]这种方式控制SpringBoot应用的生命周期。

实现方案

创建脚本文件

可以通过vim方式创建文件,文件内容如下:

#!/bin/bash

# Java ENV

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home

export JRE_HOME=${JAVA_HOME}/jre

# Apps Info

# 应用存放地址

APP_HOME=/Users/shanglishuai/temp/tyronesoft/applications

# 应用名称

APP_NAME=$1

# Shell Info

# 使用说明,用来提示输入参数

usage() {

echo "Usage: sh boot [APP_NAME] [start|stop|restart|status]"

exit 1

}

# 检查程序是否在运行

is_exist(){

# 获取PID

PID=$(ps -ef |grep ${APP_NAME} | grep -v $0 |grep -v grep |awk '{print $2}')

# -z "${pid}"判断pid是否存在,如果不存在返回1,存在返回0

if [ -z "${PID}" ]; then

# 如果进程不存在返回1

return 1

else

# 进程存在返回0

return 0

fi

}

# 定义启动程序函数

start(){

is_exist

if [ $? -eq "0" ]; then

echo "${APP_NAME} is already running, PID=${PID}"

else

nohup ${JRE_HOME}/bin/java -jar ${APP_HOME}/${APP_NAME} >/dev/null 2>&1 &

PID=$(echo $!)

echo "${APP_NAME} start success, PID=$!"

fi

}

# 停止进程函数

stop(){

is_exist

if [ $? -eq "0" ]; then

kill -9 ${PID}

echo "${APP_NAME} process stop, PID=${PID}"

else

echo "There is not the process of ${APP_NAME}"

fi

}

# 重启进程函数

restart(){

stop

start

}

# 查看进程状态

status(){

is_exist

if [ $? -eq "0" ]; then

echo "${APP_NAME} is running, PID=${PID}"

else

echo "There is not the process of ${APP_NAME}"

fi

}

case $2 in

"start")

start

;;

"stop")

stop

;;

"restart")

restart

;;

"status")

status

;;

*)

usage

;;

esac

exit 0

Shell脚本文件可以任意命名,本文将脚本命名为boot,专门用来控制SpringBoot应用的生命周期。

授权

新创建好的Shell脚本文件需要经过授权才可以使用。

授权方式:chmod +x [文件名称]

示例:

为本文脚本文件boot授权

chmod +x boot

使用方式

这个脚本可以直接使用,需要注意以下几点:

修改JDK的位置

修改SpringBoot应用的位置

使用时需要在SpringBoot应用目录执行,示例如下:

shangliuaideMBP:applications shanglishuai$ pwd

/Users/shanglishuai/temp/tyronesoft/applications

shangliuaideMBP:applications shanglishuai$ ls

nohup.outspringboot-example-0.0.1-SNAPSHOT.jar

shangliuaideMBP:applications shanglishuai$ ./../bin/boot springboot-example-0.0.1-SNAPSHOT.jar status

springboot-example-0.0.1-SNAPSHOT.jar is running, PID=3528

shangliuaideMBP:applications shanglishuai$

参考链接:

http://www.cnblogs.com/gyjx2016/p/8462929.html

https://blog.csdn.net/qq_36296997/article/details/81563204

https://blog.csdn.net/weixin_42558057/article/details/82731354

————————————————

版权声明:本文为CSDN博主「小曦阳哟」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/myNameIssls/article/details/84841567

阅读(949) | 评论(0) | 转发(0) |

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值