文章目录

* 基于 Shell 脚本启动、停止 SpringBoot 应用
<https://blog.csdn.net/myNameIssls/article/details/84841567#_Shell__SpringBoot__1>
* 需求描述 <https://blog.csdn.net/myNameIssls/article/details/84841567#_3>
* 场景分析 <https://blog.csdn.net/myNameIssls/article/details/84841567#_6>
* 实现方案 <https://blog.csdn.net/myNameIssls/article/details/84841567#_19>
* 创建脚本文件 <https://blog.csdn.net/myNameIssls/article/details/84841567#_20>
* 授权 <https://blog.csdn.net/myNameIssls/article/details/84841567#_117>
* 使用方式 <https://blog.csdn.net/myNameIssls/article/details/84841567#_124>
* 测试 <https://blog.csdn.net/myNameIssls/article/details/84841567#_140>


<>基于 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.out springboot-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.runoob.com/linux/linux-shell.html
<http://www.runoob.com/linux/linux-shell.html>
http://www.cnblogs.com/gyjx2016/p/8462929.html
<http://www.cnblogs.com/gyjx2016/p/8462929.html>
https://blog.csdn.net/qq_36296997/article/details/81563204
<https://blog.csdn.net/qq_36296997/article/details/81563204>
https://blog.csdn.net/weixin_42558057/article/details/82731354
<https://blog.csdn.net/weixin_42558057/article/details/82731354>

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信