Spring Boot 运行脚本
Spring boot部署用的脚本,真的很方便
安装日志工具
- 首先安装日志切割工具cronolog
yum install -y epel-release yum install -y cronolog
jar包运行脚本模板
#!/bin/bash
#使用说明,用来提示输入参数
usage() {
echo "Usage: sh 执行脚本.sh -s [start|stop|restart|status] -e[dev|test|prod]"
exit 1
}
export SERVER="AppName-根据jar包名称修改"
export cronolog=$(which cronolog)
export ENV="dev"
export SCRIPT=""
export BASE_DIR=$(
cd $(dirname $0)
pwd
)
export TIME=$(date "+%Y-%m-%d")
export LOGFILE="${BASE_DIR}/logs/${SERVER}-%Y-%m-%d.log"
while getopts ":s:e:" ARGS; do
case $ARGS in
e)
ENV=$OPTARG
;;
s)
SCRIPT=$OPTARG
;;
?)
echo "未知选项:$ARGS"
exit 1
;;
esac
done
# JVM Configuration
if [[ "${ENV}" == "test" ]]; then
JAVA_OPT="${JAVA_OPT} -Xms512m -Xmx512m -Xmn256m"
elif [[ "${ENV}" == "prod" ]]; then
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"
else
JAVA_OPT="${JAVA_OPT} -Xms512m -Xmx512m -Xmn256m"
fi
JAVA_OPT="${JAVA_OPT} -jar ${SERVER}.jar"
JAVA_OPT="${JAVA_OPT} --spring.profiles.active=${ENV}"
if [ ! -d "${BASE_DIR}/logs" ]; then
mkdir ${BASE_DIR}/logs
fi
#检查程序是否在运行
is_exist() {
pid=$(ps -ef | grep ${SERVER} | 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 "${SERVER} is already running. pid=${pid} ."
else
nohup java ${JAVA_OPT} 2>&1 | ${cronolog} -p days ${LOGFILE} >> /dev/null &
echo "${SERVER} is starting,active profile:${ENV},you can check it with command: tail -f ${BASE_DIR}/logs/${SERVER}-${TIME}.log"
fi
}
#停止方法
stop() {
is_exist
if [ $? -eq "0" ]; then
kill -9 $pid
else
echo "${SERVER} is not running"
fi
}
#输出运行状态
status() {
is_exist
if [ $? -eq "0" ]; then
echo "${SERVER} is running. Pid is ${pid}"
else
echo "${SERVER} is NOT running."
fi
}
#重启
restart() {
stop
start
}
#根据输入参数,选择执行对应方法,不输入则执行使用说明
case "${SCRIPT}" in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
*)
usage
;;
esac
使用
- 修改权限
chmod 755 app.sh
- 启动(环境根据实际开发的情况指定)
./app.sh -s start -e prod
- 重启
./app.sh -s restart -e prod
- 停止
./app.sh -s stop
- 运行状态
./app.sh -s status
常见问题
执行脚本文件报错 /bin/bash^M: bad interpreter: No such file or directory
这是文件格式问题:#编辑文件 vi <fileName> #执行命令 如果显示fileformat=dos,则需要修改文件格式为unix :set ff #修改格式 :set ff=unix :wq