How can the Liunx system deploy and manage SpringBoot project applications? The simplest way is to write a shell script.
Spring Boot is a popular Java framework used for developing enterprise level applications. Next, we will learn how to deploy Spring Boot applications on Linux servers and implement start, stop, restart, and other operations through a script.
Step 1: Prepare the environment
Linux system installation JDK environment: Ensure 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, such as /opt/myapp/myapp.jar.
Step 3: Create a management script
Create a new file on a Linux server, such as pzyblog.sh, and paste the following content:
#!/bin/bash
#public path
BASE_PATH="/usr/local/java/jar/api-service"
#application name (i.e jar package name, excluding suffix)
JAR_NAME="api-service"
#definition JAR the path to the file
JAR_PATH="$BASE_PATH/$JAR_NAME.jar"
#definition Java virtual machine options
JAVA_OPTS="-Xms256m -Xmx512m"
#definition PID custom usage of file path
PID_PATH="/usr/local/java/jar/api-service/$JAR_NAME.pid"
#define the path to the log file
LOG_PATH="$BASE_PATH/$JAR_NAME.log"
start() {
#check if it has been run
if [ -f $PID_PATH ] && ps -p $(cat $PID_PATH) > /dev/null; then
echo "the application is already running."
exit 0
fi
#start the application
echo "start launching application"
nohup java $JAVA_OPTS -jar $JAR_PATH > $LOG_PATH 2>&1 &
echo $! > $PID_PATH
#check if the application is started
sleep 5 #wait for a few seconds to ensure that the application has time to start
if ps -p $(cat $PID_PATH) > /dev/null; then
echo "application started successfully ."
else
echo "application launch failed ."
exit 1
fi
}
stop() {
#check if it is running
if [ ! -f $PID_PATH ] || ! ps -p $(cat $PID_PATH) > /dev/null; then
echo "the application is not running."
exit 0
fi
#stop application
echo "start stopping application ."
kill $(cat $PID_PATH)
rm $PID_PATH
echo "the application has stopped successfully ."
}
restart() {
stop
start
}
#execute corresponding operations based on command line parameters
case $1 in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo "usage : $0 {start|stop|restart}"
exit 1
;;
esac
Please make 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 script execution permissions
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 use the following commands to start, stop, and restart your Spring Boot application:
- Start:
./pzyblog.sh start - Stop:
./pzyblog.sh stop - Restart:
./pzyblog.sh restart
Conclusion
This script provides a simple yet powerful solution for effectively managing your Spring Boot application. By fine-tuning several key variables, you can easily adjust them to fit your personal project.
Although deploying and managing Java applications may sound complicated, as long as you master the basic steps and choose appropriate tools, even beginners can successfully use them. May this article not only help you get started quickly, but also provide great help!
First article: https://www.panziye.com/java/8162.html.