🗒️Linux下运行SpringBoot Jar包Shell脚本
2017-10-30
| 2023-7-13
0  |  0 分钟
type
status
date
slug
summary
tags
category
icon
password
标签

背景

SpringBoot项目打成Jar包后是直接用 java -jar命令行来运行的,放到Linux服务器上作为应用服务来运行时启动、停止、重启都特别麻烦。专门写了个 Shell脚本方便在Linux服务器上对SpringBoot项目的Jar包进行启动、停止、重启操作。

Shell脚本

#!/bin/sh # Linux startup script for Spring Boot App . /etc/rc.d/init.d/functions # jdk 目录 JAVA_HOME=/usr/java/jdk1.8.0_144 # jar 包目录 JAR_HOME=/data/springcloud/server export JAVA_HOME JAR_HOME # jar 包名 JAR_NAME="server-1.0.jar" # 服务端口号 SERVER_PORT=9001 JAVA=$JAVA_HOME/bin/java LANG=zh_CN.GBK export LANG ARGS="--server.port=$SERVER_PORT" force_stop() { ps auxwww|grep $JAR_NAME >/dev/null if [ $? = 0 ]; then echo "force stop..." PID=$(ps -ef | grep $JAR_NAME | grep -v grep | awk '{ print $2 }') if [ -n "$PID" ];then kill -9 $PID if [ $? = 0 ];then action $"Force stop server PID:$PID :" /bin/true else action $"Force stop server PID:$PID :" /bin/false fi else echo "no PID found,the server has been stopped." fi else echo "Process keyword: $JAR_NAME not found.NO Server started?" fi } case "$1" in start) ## clear jvm-log test -e "$JAR_HOME/logs/app.log" && /bin/cp -f $JAR_HOME/logs/app.log $JAR_HOME/logs/app.log.$(date "+%Y%m%d%H%M%S") test -e "$JAR_HOME/logs/app.log" && cat /dev/null >$JAR_HOME/logs/app.log nohup $JAVA -jar $JAR_HOME/$JAR_NAME $ARGS > /dev/null 2>&1 & ret=$? if [ $ret -eq 0 ]; then action $"Starting $JAR_NAME: " /bin/true else action $"Starting $JAR_NAME: " /bin/false cat $JAR_HOME/logs/start.info fi ;; stop) force_stop ;; restart) $0 stop $0 start ;; *) echo "Usage: $0 {start|stop|restart}" exit 1 esac exit 0

使用

# 启动 ./server.sh start # 停止 ./server.sh stop # 重启 ./server.sh restart
DevOps
  • SpringBoot
  • 用Java和Nodejs获取http30X跳转后的url如何在Linux安装RabbitMQ
    目录