Gracefully start and stop Spring Boot projects in Linux

Gracefully start and stop Spring Boot projects in Linux

This article will introduce how to gracefully start and stop the Spring Boot project in the Linux system. Use the nohup, screenand systemdmethods. Each method has its advantages, and which method to choose depends on your specific needs and usage scenarios.

1. Use nohup to run the Spring Boot project in the background

First, package the Spring Boot project into a JAR file. Execute the following commands depending on the build tool you are using:

Maven project:

mvn clean package

Gradle project:

gradle clean build

Next, start the project with the nohup command and redirect the output to a log file:

nohup java -jar /path/to/your-spring-boot-app.jar > /path/to/your-log-file.log 2>&1 &

/path/to/your-spring-boot-app.jaris the path to the JAR file generated by your Spring Boot project, /path/to/your-log-file.logand is the path where you want to store the log files.

Now, the Spring Boot project will run as a background process, unaffected by terminal closing.

stop gracefully

To find the process ID of a Spring Boot project, use the following command:

ps -ef | grep your-spring-boot-app.jar

Find the process ID (usually the second column).

Send a SIGTERM signal (signal 15) to gracefully stop a Spring Boot project with the following command:

kill -15 <process-id>

Here, <process-id>is the process ID you found in the first step.

Sending a SIGTERM signal allows a Spring Boot project to perform cleanup operations such as closing database connections, releasing resources, etc. This ensures that the Spring Boot project stops gracefully.

2. Use screen to run the Spring Boot project in the background

screen is a terminal multiplexer for switching between multiple processes. First, make sure your system has screen installed. If not, you can install it with the following command:

For Debian based systems (such as Ubuntu):

sudo apt-get install screen

For RHEL based systems (such as CentOS):

sudo yum install screen

Once installed, use screen to create a new session:

screen -S your-session-name

In the newly created session, start your Spring Boot project. Next, press Ctrl+A followed by D to detach the session from the current terminal. This way you can close the terminal and the Spring Boot project will continue to run in the background.

If you want to reconnect to a previous session, you can use the following command:

screen -r your-session-name

3. Use systemd to run the Spring Boot project in the background

systemdis the default init system on modern Linux systems. By creating a systemdservice, you can run a Spring Boot project as a background process and manage it conveniently.

First, create a new systemdconfiguration file, for example /etc/systemd/system/your-spring-boot-app.service, and fill it with content using the following template:

[Unit]
Description=Your Spring Boot Application
After=syslog.target network.target

[Service]
User=your-user
ExecStart=/usr/bin/java -jar /path/to/your-spring-boot-app.jar
SuccessExitStatus=143
Restart=always

[Install]
WantedBy=multi-user.target

Note the replacement your-user, /path/to/your-spring-boot-app.jarand service description.

Then, execute the following command so that systemdthe new service is recognized:

sudo systemctl daemon-reload

Next, start your Spring Boot project:

sudo systemctl start your-spring-boot-app.service

To turn on autostart, run:

sudo systemctl enable your-spring-boot-app.service

To view the status and logs of a Spring Boot project, execute:

sudo systemctl status your-spring-boot-app.service
sudo journalctl -u your-spring-boot-app.service -f

stop gracefully

Gracefully stop the Spring Boot project with the following command:

sudo systemctl stop your-spring-boot-app.service

Here, your-spring-boot-app.serviceis the name of the service file you created for your Spring Boot project systemd.

When you use systemctl stopthe command, systemda SIGTERM signal is sent to the Spring Boot project. This allows Spring Boot projects to perform cleanup operations such as closing database connections, releasing resources, etc. This ensures that the Spring Boot project stops gracefully.

To see the status of a Spring Boot project, execute:

sudo systemctl status your-spring-boot-app.service

This will show the current status of the service, including whether it is stopped.

Summarize

nohupThis article describes how to use , screenand systemdrun in the background and gracefully stop a Spring Boot project on a Linux system . Each method has its advantages, and which method to choose depends on your specific needs and usage scenarios. I hope this article can help you deploy and manage Spring Boot projects in actual projects. If you have any questions, welcome to comment.

Guess you like

Origin blog.csdn.net/kaka_buka/article/details/130509952