spingboot jar 包启动遇到得坑
先摘抄一篇文章
pringboot打成jar包后,可直接用java -jar app.jar
启动,或者使用 nohup java -jar app.jar &
后台启动,也可以将 jar包链接到/etc/init.d/下,作为服务启动 ,这两种启动方式各有缺点,可以用一个shell脚本解决这个问题
将文章最后脚本保存为sh文件,比如 spring-boot.sh
,跟jar包放在同一目录下,给与执行权限 chomd 777 spring-boot.sh
.使用方法如下:
切换到jar目录,执行:
sh spring-boot.sh start app.jar --spring.profiles.active=peer1
解释:
第一个参数: 启动命令 (start,stop,restart,status)
第二个参数: 需启动的jar包
第三个参数: 启动参数(可不加)
脚本中的jvm参数可以根据需求修改
#!/bin/bash
SpringBoot=$2
#启动参数
START_OPTS=$3
#JVM参数
JVM_OPTS="-Dname=$SpringBoot -Duser.timezone=Asia/Shanghai -Xms512M -Xmx512M -XX:PermSize=256M -XX:MaxPermSize=512M -XX:+HeapDumpOnOutOfMemoryError -XX:+PrintGCDateStamps -XX:+PrintGCDetails -XX:NewRatio=1 -XX:SurvivorRatio=30 -XX:+UseParallelGC -XX:+UseParallelOldGC"
APP_HOME=`pwd`
LOG_PATH=$APP_HOME/logs/$SpringBoot.log
if [ "$1" = "" ];
then
echo -e "\033[0;31m 未输入操作名 \033[0m \033[0;34m {start|stop|restart|status} \033[0m"
exit 1
fi
if [ "$SpringBoot" = "" ];
then
echo -e "\033[0;31m 未输入应用名 \033[0m"
exit 1
fi
function start()
{
count=`ps -ef |grep java|grep $SpringBoot|grep -v grep|wc -l`
if [ $count != 0 ];then
echo "$SpringBoot is running..."
else
echo "Start $SpringBoot success..."
nohup java -jar $JVM_OPTS $SpringBoot $START_OPTS > /dev/null 2>&1 &
fi
}
function stop()
{
echo "Stop $SpringBoot"
boot_id=`ps -ef |grep java|grep $SpringBoot|grep -v grep|awk '{print $2}'`
count=`ps -ef |grep java|grep $SpringBoot|grep -v grep|wc -l`
if [ $count != 0 ];then
kill $boot_id
count=`ps -ef |grep java|grep $SpringBoot|grep -v grep|wc -l`
boot_id=`ps -ef |grep java|grep $SpringBoot|grep -v grep|awk '{print $2}'`
kill -9 $boot_id
fi
}
function restart()
{
stop
sleep 2
start
}
function status()
{
count=`ps -ef |grep java|grep $SpringBoot|grep -v grep|wc -l`
if [ $count != 0 ];then
echo "$SpringBoot is running..."
else
echo "$SpringBoot is not running..."
fi
}
case $1 in
start)
start;;
stop)
stop;;
restart)
restart;;
status)
status;;
*)
echo -e "\033[0;31m Usage: \033[0m \033[0;34m sh $0 {start|stop|restart|status} {SpringBootJarName} \033[0m
\033[0;31m Example: \033[0m
\033[0;33m sh $0 start esmart-test.jar \033[0m"
esac
其中出现得错误如下 1. 启动命令需要更改 nohup java -jar $springboot > /dev/null 2>&1 &
2. 赋予权限问题 命为 chmod 并非本文中得 chomd