SpringBoot项目生成和部署
1、项目生成
2、启动项目
jar包启动方式 |
说明 |
java -jar xxx.jar |
ctrl+c进程也会自动关闭,静默运行可在最后加& |
nohup java -jar xxx.jar --spring.profiles.active=test > /dev/null 2>&1 & |
可选参数--spring.profiles.active=test设置环境变量 |
nohup java -jar xxx.jar 2>&1 & |
实现屏幕输出记录到日志文件 |
nohup java -jar xxx.jar >myout.log 2>&1 & nohup java -jar springboot.jar --server.port=8181 >outlog.log 2>&1 & |
将日志输出到别的文件中 |
nohup java -Dserver.port=8086 -Dspring.config.additional-location=./application-dev.yml -jar ./springboot.jar> nohup.out 2>&1 & |
加载服务器配置文件application-dev.yml |
3、启动和关闭脚本
1)启动
#!/bin/sh
#
# 启动 jar 运行
# 项目部署目录
projectDir=/opt/springboot/
# 项目运行 jar 名称
jarName="springbootdemo.jar"
# 脚本日志目录
logDir=/var/log/springbootdemo/
# 项目部署环境
profileActive=dev
# 这里的-x 参数判断${logDir}是否存在并且是否具有可执行权限
if [ ! -x "${logDir}" ]; then
mkdir -p "${logDir}"
fi
# 判断项目SpringBoot程序是否运行
count=$(ps -ef |grep ${jarName} |grep -v "grep" |wc -l)
if [ ${count} -lt 1 ]; then
cd ${projectDir}
nohup java -jar ${jarName} --spring.profiles.active=${profileActive} > /dev/null 2>&1 &
echo "$(date '+%Y-%m-%d %H:%M:%S') 启动 ${jarName} 程序 ... ..." >> ${logDir}$(date "+%Y-%m-%d").log
else
echo "$(date '+%Y-%m-%d %H:%M:%S') ${jarName} 程序运行正常 !!! !!!" >> ${logDir}$(date "+%Y-%m-%d").log
fi
2)关闭
#!/bin/sh
#
# 停止 jar 运行
# 项目部署目录
projectDir="/opt/springboot/"
# 项目运行 jar 名称
jarName="springbootdemo.jar"
# 脚本名称
scriptName="stop-springboot.sh"
# 判断项目SpringBoot程序是否运行
count=$(ps -ef |grep ${jarName} |grep -v "grep" |wc -l)
if [ ${count} -gt 0 ]; then
echo "已经存在 ${count} 个${jarName} 程序在运行"
# 获取正在运行的程序进程 id(排除 grep 本身、awk 命令以及脚本本身)
jarPid=$(ps x | grep ${jarName} | grep -v grep | grep -v '${scriptName}' | awk '{print $1}')
# 停止正在运行的项目进程
kill -9 ${jarPid}
output=`echo "正在关闭${jarName}程序,进程id: ${jarPid}"`
echo ${output}
else
echo '没有对应的程序在运行'
fi
# 删除 jar 包
rm -rf ${projectDir}${jarName}
# 进入 项目部署目录
cd ${projectDir}