How does the Liunx system deploy and manage SpringBoot project applications? The easiest way is to write a shell script.
Spring Boot is a popular framework for Java used for developing enterprise-level applications. Below we will learn how to deploy a Spring Boot application on a Linux server and implement start, stop, restart and other operations through a script.
Step 1: Prepare the environment
Install JDK environment on Linux system: Make sure that the appropriate version of Java is installed on your Linux server. Verify by running java -version
.
Step 2: Upload your JAR file
Upload the JAR file of your Spring Boot application to a directory on the server, for example /opt/myapp/myapp.jar
.
Step 3: Create management scripts
Linux服务器
Create a new file on, for example , pzyblog.sh
and paste the following content:
#!/bin/bash
# 公共路径
BASE_PATH="/usr/local/java/jar/api-service"
# 应用名称(就是jar包名称,不包含后缀)
JAR_NAME="api-service"
# 定义JAR文件的路径
JAR_PATH="$BASE_PATH/$JAR_NAME.jar"
# 定义Java虚拟机的选项
JAVA_OPTS="-Xms256m -Xmx512m"
# 定义PID文件的路径 自定义用法
PID_PATH="/usr/local/java/jar/api-service/$JAR_NAME.pid"
# 定义日志文件的路径
LOG_PATH="$BASE_PATH/$JAR_NAME.log"
start() {
# 检查是否已运行
if [ -f $PID_PATH ] && ps -p $(cat $PID_PATH) > /dev/null; then
echo "应用程序已经在运行。"
exit 0
fi
# 启动应用
echo "开始启动应用程序"
nohup java $JAVA_OPTS -jar $JAR_PATH > $LOG_PATH 2>&1 &
echo $! > $PID_PATH
# 检查应用是否启动
sleep 5 # 等待一些秒来确保应用有时间启动
if ps -p $(cat $PID_PATH) > /dev/null; then
echo "应用程序启动成功."
else
echo "应用程序启动失败."
exit 1
fi
}
stop() {
# 检查是否正在运行
if [ ! -f $PID_PATH ] || ! ps -p $(cat $PID_PATH) > /dev/null; then
echo "应用程序没有在运行。"
exit 0
fi
# 停止应用
echo "开始停止应用程序."
kill $(cat $PID_PATH)
rm $PID_PATH
echo "应用程序已停止成功."
}
restart() {
stop
start
}
# 基于命令行参数执行相应操作
case $1 in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo "用法: $0 {start|stop|restart}"
exit 1
;;
esac
Be sure to modify the values of variables such as JAR_PATH
, JAVA_OPTS
, PID_PATH
and LOG_PATH
to match your specific situation.
Step 4: Grant execution permissions to the script
In the terminal, navigate to the directory where the script is located and run the following command to grant execution permissions:
chmod +x pzyblog.sh
Step 5: Use scripts to manage applications
You can now start, stop and restart your Spring Boot application using the following commands:
- start up:
./pzyblog.sh start
- stop:
./pzyblog.sh stop
- Restart:
./pzyblog.sh restart
in conclusion
This script provides a simple yet powerful solution for effectively managing your Spring Boot application. By fine-tuning a few key variables, you can easily adapt it to your personal project.
Although deploying and managing Java applications may sound complicated, even novices can successfully use it as long as they understand the basic steps and choose the right tools. I hope this article will help you get started quickly and provide you with great help!
This article was first published: https://www.panziye.com/java/8162.html