Java Shell springboot通用Shell启动脚本
前言
Spring boot 通用启动脚本,包含启动,停止,重新启动,查看状态命令参数。此脚本能自动寻找与脚本在目录下的jar包做为程序启动包。自动检测java环境。无需一定要在程序目录下执行脚本
Spring Boot Linux启动脚本
#!/bin/bash
SCRIPT_CALL_DIR=$(pwd)/
SCRIPT_FILENAME=`readlink -f $0`
SCRIPT_DIR=${SCRIPT_FILENAME%/*}
JAVA_CMD=`command -v java`
JAVA_OPTS="-Xms512m -Xmx512m -Dfile.encoding=utf-8"
#这里可替换为你自己的执行程序,其他代码无需更改,默认查找此脚本同目录jar文件,只取第一个
APP_NAME=`ls -l ${SCRIPT_DIR}|grep -e ".*[.]jar$"|head -n 1|awk '{print $9}'`
[ ! -z $APP_NAME ] && APP_NAME=$SCRIPT_DIR/$APP_NAME
echo "============================================================"
echo "SCRIPT_CALL_DIR: $SCRIPT_CALL_DIR"
echo "SCRIPT_DIR: $SCRIPT_DIR"
echo "SCRIPT_FILENAME: $SCRIPT_FILENAME"
echo "JAVA_CMD: $JAVA_CMD"
echo "JAVA_OPTS: $JAVA_OPTS"
echo "APP_NAME: $APP_NAME"
echo "============================================================"
[ ! -e $JAVA_CMD ] && echo "没有安装java运行环境" && exit 1
[ -z $APP_NAME ] && echo "没有找到jar程序包" && exit 1
#使用说明,用来提示输入参数
usage() {
echo "Usage: sh startup.sh [start|stop|restart|status]"
exit 1
}
#检查程序是否在运行
is_exist(){
pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}'`
#如果不存在返回1,存在返回0
if [ -z "${pid}" ]; then
return 1
else
return 0
fi
}
#启动方法
start(){
is_exist
if [ $? -eq 0 ]; then
echo "${APP_NAME} is already running. pid=${pid}"
else
#nohup java -jar ${APP_NAME} &
#Xms Xmx 指定虚拟内存上下限
#nohup java -jar ${APP_NAME} >csls.out 2>&1 &
#nohup java -Xms512m -Xmx512m -Dfile.encoding=utf-8 -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=<ip>:<port> -Dspring.profiles.active=test -jar ${APP_NAME} > output.log 2>&1 &
cd ${SCRIPT_DIR}
nohup ${JAVA_CMD} ${JAVA_OPTS} -jar ${APP_NAME} > /dev/null 2>&1 &
is_exist
if [ $? -eq 0 ]; then
echo "${APP_NAME} is running success. pid=${pid}"
fi
fi
}
#停止方法
stop(){
is_exist
if [ $? -eq "0" ]; then
kill -9 $pid
is_exist
if [ $? -eq 0 ]; then
echo "${APP_NAME} still in the running. pid=${pid}"
else
echo "${APP_NAME} has stopped running."
fi
else
echo "${APP_NAME} is not running"
fi
}
#输出运行状态
status(){
is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} is running. Pid is ${pid}"
else
echo "${APP_NAME} is NOT running."
fi
}
#重启
restart(){
stop
start
}
#根据输入参数,选择执行对应方法,不输入则执行使用说明
case "$1" in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
*)
usage
;;
esac
tip: IDEA 远程代码调适
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=<ip>:<port>
使用示例及说明
此脚本要与运行的springboot包放在同一目录下,目录结构如下
.
├── config
│ └── application.yml
├── data-batch-service.jar
└── startup.sh
运行sh startup.sh
sh startup.sh
结果如下
[dsp@tbase01 data-batch-service]$ sh startup.sh
============================================================
SCRIPT_CALL_DIR: /home/dsp/veshivue/pingan_weiwai/data-batch-service/
SCRIPT_DIR: /home/dsp/veshivue/pingan_weiwai/data-batch-service/
SCRIPT_FILENAME: startup.sh
JAVA_CMD: /tpmdm/software/service/jdk-11.0.6/bin/java
JAVA_OPTS: -Xms512m -Xmx512m -Dfile.encoding=utf-8
APP_NAME: /home/dsp/veshivue/pingan_weiwai/data-batch-service/data-batch-service.jar
============================================================
Usage: sh startup.sh [start|stop|restart|status]
运行sh startup.sh start
sh startup.sh start
结果如下
[dsp@tbase01 data-batch-service]$ sh startup.sh start
============================================================
SCRIPT_CALL_DIR: /home/dsp/veshivue/pingan_weiwai/data-batch-service/
SCRIPT_DIR: /home/dsp/veshivue/pingan_weiwai/data-batch-service/
SCRIPT_FILENAME: startup.sh
JAVA_CMD: /tpmdm/software/service/jdk-11.0.6/bin/java
JAVA_OPTS: -Xms512m -Xmx512m -Dfile.encoding=utf-8
APP_NAME: /home/dsp/veshivue/pingan_weiwai/data-batch-service/data-batch-service.jar
============================================================
/home/dsp/veshivue/pingan_weiwai/data-batch-service/data-batch-service.jar is already running. pid=470201
运行sh startup.sh stop
sh startup.sh stop
结果如下
[dsp@tbase01 data-batch-service]$ sh startup.sh stop
============================================================
SCRIPT_CALL_DIR: /home/dsp/veshivue/pingan_weiwai/data-batch-service/
SCRIPT_DIR: /home/dsp/veshivue/pingan_weiwai/data-batch-service/
SCRIPT_FILENAME: startup.sh
JAVA_CMD: /tpmdm/software/service/jdk-11.0.6/bin/java
JAVA_OPTS: -Xms512m -Xmx512m -Dfile.encoding=utf-8
APP_NAME: /home/dsp/veshivue/pingan_weiwai/data-batch-service/data-batch-service.jar
============================================================
/home/dsp/veshivue/pingan_weiwai/data-batch-service/data-batch-service.jar has stopped running.
运行sh startup.sh restart
sh startup.sh restart
结果如下
[dsp@tbase01 data-batch-service]$ sh startup.sh restart
============================================================
SCRIPT_CALL_DIR: /home/dsp/veshivue/pingan_weiwai/data-batch-service/
SCRIPT_DIR: /home/dsp/veshivue/pingan_weiwai/data-batch-service/
SCRIPT_FILENAME: startup.sh
JAVA_CMD: /tpmdm/software/service/jdk-11.0.6/bin/java
JAVA_OPTS: -Xms512m -Xmx512m -Dfile.encoding=utf-8
APP_NAME: /home/dsp/veshivue/pingan_weiwai/data-batch-service/data-batch-service.jar
============================================================
/home/dsp/veshivue/pingan_weiwai/data-batch-service/data-batch-service.jar is not running
/home/dsp/veshivue/pingan_weiwai/data-batch-service/data-batch-service.jar is running success. pid=470863
运行sh startup.sh status
sh startup.sh status
结果如下
[dsp@tbase01 data-batch-service]$ sh startup.sh status
============================================================
SCRIPT_CALL_DIR: /home/dsp/veshivue/pingan_weiwai/data-batch-service/
SCRIPT_DIR: /home/dsp/veshivue/pingan_weiwai/data-batch-service/
SCRIPT_FILENAME: startup.sh
JAVA_CMD: /tpmdm/software/service/jdk-11.0.6/bin/java
JAVA_OPTS: -Xms512m -Xmx512m -Dfile.encoding=utf-8
APP_NAME: /home/dsp/veshivue/pingan_weiwai/data-batch-service/data-batch-service.jar
============================================================
/home/dsp/veshivue/pingan_weiwai/data-batch-service/data-batch-service.jar is running. Pid is 470863