Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3283164
  • 博文数量: 515
  • 博客积分: 5116
  • 博客等级: 大校
  • 技术积分: 4137
  • 用 户 组: 普通用户
  • 注册时间: 2006-09-30 11:24
个人简介

黑马王子就是我! 技术认证:系统分析师,网络规划设计师,网络工程师,信息系统监理师,系统集成项目管理师,初级程序员,MCSE,MCDBA,CCNA 目前主攻虚拟化技术,VPN,系统架构,集群和高可用性等。

文章分类

全部博文(515)

文章存档

2023年(11)

2022年(17)

2021年(19)

2020年(28)

2019年(42)

2018年(53)

2017年(41)

2016年(33)

2015年(43)

2014年(25)

2013年(13)

2011年(6)

2010年(28)

2009年(47)

2008年(55)

2007年(26)

2006年(28)

分类: 系统运维

2019-09-07 14:44:41

版权声明:本文为博主原创文章,遵循 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.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.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
阅读(2360) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~