springboot发布脚本

启动脚本 startup.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#! /bin/bash

#===========================================================================================
# 基础配置
#===========================================================================================
# 项目名
export APPLICATION="eachbot-kbs"
# 项目启动jar包名称
export APPLICATION_JAR=${APPLICATION}.jar
# Java指令路径
export JAVA="$JAVA_HOME/bin/java"
# 脚本上级绝对目录($0:当前脚本的文件名, $1:代表输入的第一个参数, $2:代表输入的第二个参数)
export BASE_DIR=`cd $(dirname $0); pwd`
# 默认配置文件路径
export DEFAULT_SEARCH_LOCATIONS="classpath:/,classpath:/config/,file:./,file:./config/"
# 配置文件路径
export CONFIG_DIR=file:${BASE_DIR}/conf/,${DEFAULT_SEARCH_LOCATIONS}
# 获取PID指令(PID = $(eval $PID_CMD))
export PID_CMD="ps -ef |grep $APPLICATION_JAR |grep -v grep |awk '{print \$2}'"



#===========================================================================================
# JVM 虚拟机配置
#===========================================================================================
JAVA_OPT="${JAVA_OPT} -server -Xms2g -Xmx2g -Xmn1g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${BASE_DIR}/logs/java_heapdump.hprof"
JAVA_OPT="${JAVA_OPT} -XX:-UseLargePages"

JAVA_VERSION=$($JAVA -version 2>&1 | sed -E -n 's/.* version "([0-9]*).*$/\1/p')
echo "java version: $JAVA_VERSION"

if [[ "$JAVA_VERSION" -ge "9" ]] ; then
JAVA_OPT="${JAVA_OPT} -Xlog:gc*:file=${BASE_DIR}/logs/gc.log:time,tags:filecount=10,filesize=102400"
else
JAVA_OPT="${JAVA_OPT} -Djava.ext.dirs=${JAVA_HOME}/jre/lib/ext:${JAVA_HOME}/lib/ext"
JAVA_OPT="${JAVA_OPT} -Xloggc:${BASE_DIR}/logs/gc.log -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=100M"
fi

# 启动springboot服务
JAVA_OPT="${JAVA_OPT} -jar ${BASE_DIR}/${APPLICATION_JAR}"
#JAVA_OPT="${JAVA_OPT} --spring.config.location=${CUSTOM_SEARCH_LOCATIONS}"
#JAVA_OPT="${JAVA_OPT} --logging.config=${BASEDIR}/conf/logback-custom.xml"

JAVA_OPT="${JAVA_OPT} --server.max-http-header-size=524288"

if [ ! -d "${BASE_DIR}/logs" ]; then
mkdir ${BASE_DIR}/logs
fi

if [ ! -f "${BASE_DIR}/logs/start.out" ]; then
touch "${BASE_DIR}/logs/start.out"
fi


#===========================================================================================
# 输出配置信息
#===========================================================================================
# 当前时间
NOW=$(date +"%Y-%m-%m %H:%M:%S")
# 输出当前启动时间
echo "================================================ ${NOW} ================================================"
# 显示系统信息
echo "system info: `uname`"
# 输出项目名称
echo "application name: ${APPLICATION}"
# 输出jar包名称
echo "application jar name: ${APPLICATION_JAR}"
# 输出项目根目录
echo "application root path: ${BASE_DIR}"
# 输出项目bin路径
echo "application bin path: ${BASE_DIR}"
# 输出配置文件路径
echo "application conf path: ${CONFIG_DIR}"
# 启动日志路径
echo "application start logs: ${BASE_DIR}/logs"
# 输出java启动参数
echo "application start cmd: $JAVA ${JAVA_OPT} > ${BASE_DIR}/logs/start.out 2>&1 &"


#===========================================================================================
# 启动服务
#===========================================================================================
PID=$(eval $PID_CMD)
if [[ -n $PID ]]; then
echo "$APPLICATION is already running,PID is $PID"
else
nohup $JAVA ${JAVA_OPT} > /dev/null 2>&1 &
PID=$(eval $PID_CMD)
if [[ -n $PID ]]; then
echo "Start $APPLICATION successfully, PID is $PID, you can check the ${BASE_DIR}/logs/start.out"
else
echo "Failed to start $APPLICATION !!!"
fi
fi

停止脚本 shutdown.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#! /bin/bash

# 项目名
export APPLICATION="eachbot-kbs"
# 项目启动jar包名称
export APPLICATION_JAR=${APPLICATION}.jar
# 查找进程指令
export PID_CMD="ps -ef |grep $APPLICATION_JAR |grep -v grep |awk '{print \$2}'"

PID=$(eval $PID_CMD)

if [[ -z $PID ]]
then
echo ${APPLICATION} is already stopped
else
echo kill ${PID}
kill -9 ${PID}
echo ${APPLICATION} stopped successfully
fi