博客
关于我
Linux shell脚本启动springboot项目
阅读量:372 次
发布时间:2019-03-04

本文共 3973 字,大约阅读时间需要 13 分钟。

前提:jar与lib依赖分离,且处于同级目录,如果不是,可以自己修改配置文件(nohup java -jar xxx.jar)

运行实例:

执行命令:# sh start.sh [start|stop|restart|status|debug] [version] || [ADD_PORT]# ADD_PORT为debug调试模式时用,默认监听端口为51135启动程序:        sh start.sh start test-1.x.xdebug启动:      sh start.sh debug test-1.x.x | sh start.sh debug test-1.x.x 51235重启程序:        sh start.sh restart test-1.x.x查看运行状态:     sh start.sh status test-1.x.x关闭程序:        sh start.sh stop test-1.x.x获取使用教程:     sh start.sh -h[root@demo demos]# lsstart.sh lib test-1.0.jar[root@demo demos]# sh start.sh start test-1.0Service test-1.0.jar is starting!pid=17827.................Start success.................[root@demo demos]# sh start.sh status test-1.0Service test-1.0.jar is running. It's pid=17827[root@demo demos]# sh start.sh restart test-1.0.................Restarting.................Service test-1.0.jar is starting!pid=19134.................Start success..................................Restart success.................[root@demo demos]# sh start.sh stop test-1.0Service stop success!pid:19134 which has been killed forcibly!

start.sh脚本

#!/bin/sh# 定义变量JAR_NAME="$2.jar"# 监听端口ADD_PORT="$3"# help获取使用方法help() {     echo ""  echo "please use command: sh start.sh [start|stop|restart|status|debug] [version] || [ADD_PORT]"  echo "For example: sh start.sh start 1.0.1"  echo "debug For example: sh start.sh debug demo1.0 | sh start.sh debug demo1.0 6156"  echo ""  exit 1}# 该方法会重新启动程序debug() {     # 查看pid,先杀掉,再运行jar  pid=$(ps -ef | grep $JAR_NAME | grep -v grep | awk '{print $2}')  # 设置默认监听端口  if [ -z $ADD_PORT ]; then    ADD_PORT="51135"    echo "......default Listen on port for DEBUG:${ADD_PORT}"  fi  # -z 表示为空  if [ ! -z $pid ]; then    kill -9 $pid    echo ""    echo "......kill -9 ${pid}....."    nohup java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=$ADD_PORT -Dloader.path=lib/ -jar $JAR_NAME >debugOut.log 2>&1 &    pid=$(ps -ef | grep $JAR_NAME | grep -v grep | awk '{print $2}')    echo ""    echo "debug Service ${JAR_NAME} is starting!newPid:${pid}, Listen on port:${ADD_PORT}"    echo "......debug restart: success......"    echo ""  else     nohup java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=$ADD_PORT -Dloader.path=lib/ -jar $JAR_NAME >debugOut.log 2>&1 &    pid=$(ps -ef | grep $JAR_NAME | grep -v grep | awk '{print $2}')    echo ""    echo "debug Service ${JAR_NAME} is starting!pid:${pid}, address:${ADD_PORT}"    echo "......debug Start: success......"    echo ""  fi}# 启动方法start() {     # 重新获取一下pid  pid=$(ps -ef | grep $JAR_NAME | grep -v grep | awk '{print $2}')  # -z 表示为空  if [ -z $pid ]; then    nohup java -Dloader.path=lib/ -jar $JAR_NAME >out.log 2>&1 &    pid=$(ps -ef | grep $JAR_NAME | grep -v grep | awk '{print $2}')    echo ""    echo "Service ${JAR_NAME} is starting!pid:${pid}"    echo ".........Start success........"  else    echo ""    echo "Service ${JAR_NAME} is already running,it's pid:${pid}. If necessary."    echo "please use command:[sh start.sh stop [version] | sh start.sh restart [version]]."    echo ""  fi}# 停止方法stop() {     # 重新获取一下pid  pid=$(ps -ef | grep $JAR_NAME | grep -v grep | awk '{print $2}')  # -z 表示为空  if [ -z $pid ]; then    echo ""    echo "Service ${JAR_NAME} is not running!"    echo ""  else    kill -9 $pid    echo ""    echo "Service stop success!pid:${pid}"    echo ""  fi}# 输出运行状态方法status() {     # 重新获取一下pid  pid=$(ps -ef | grep $JAR_NAME | grep -v grep | awk '{print $2}')  # -z 表示为空  if [ -z $pid ]; then    echo ""    echo "Service ${JAR_NAME} is not running!"    echo ""  else    echo ""    echo "Service ${JAR_NAME} is running. It's pid=${pid}"    echo ""  fi}# 重启方法restart() {     echo ".................Restarting................."  pid=$(ps -ef | grep $JAR_NAME | grep -v grep | awk '{print $2}')  # -z 表示为空  if [ ! -z $pid ]; then    kill -9 $pid  fi  start  echo ""}# 根据输入参数执行对应方法case "$1" in"start")  start  ;;"stop")  stop  ;;"status")  status  ;;"restart")  restart  ;;"-h")  help  ;;"debug")  debug  ;;esac

转载地址:http://aakg.baihongyu.com/

你可能感兴趣的文章
localhost:5000在MacOS V12(蒙特利)中不可用
查看>>
logstash mysql 准实时同步到 elasticsearch
查看>>
Luogu2973:[USACO10HOL]赶小猪
查看>>
mabatis 中出现< 以及> 代表什么意思?
查看>>
Mac book pro打开docker出现The data couldn’t be read because it is missing
查看>>
MAC M1大数据0-1成神篇-25 hadoop高可用搭建
查看>>
mac mysql 进程_Mac平台下启动MySQL到完全终止MySQL----终端八步走
查看>>
Mac OS 12.0.1 如何安装柯美287打印机驱动,刷卡打印
查看>>
MangoDB4.0版本的安装与配置
查看>>
Manjaro 24.1 “Xahea” 发布!具有 KDE Plasma 6.1.5、GNOME 46 和最新的内核增强功能
查看>>
mapping文件目录生成修改
查看>>
MapReduce程序依赖的jar包
查看>>
mariadb multi-source replication(mariadb多主复制)
查看>>
MariaDB的简单使用
查看>>
MaterialForm对tab页进行隐藏
查看>>
Member var and Static var.
查看>>
memcached高速缓存学习笔记001---memcached介绍和安装以及基本使用
查看>>
memcached高速缓存学习笔记003---利用JAVA程序操作memcached crud操作
查看>>
Memcached:Node.js 高性能缓存解决方案
查看>>
memcache、redis原理对比
查看>>