A piece of soy sauce, sort out some of the ways that spring boot background runs. Before we introduce the background run configuration, let's review some of the ways that the spring boot app works:
- Run the Spring boot application main class
- Use MAVEN's spring boot plugin
mvn spring-boot:run
to run
- After hitting the jar package, use the
java -jar
run
When we develop, we usually use the first two, but we often use a third when we deploy. However, we are java -jar
not running in the background when we use it to run. Let's take a look at the windows and Linux/unix two environments and how to configure how the background runs.
Windows
Windows is simple, we can directly use this software: AlwaysUp
. As shown, simple, violent, and useful.
The configuration is simple, we just need to put the spring boot app mvn install
into a jar package and then write a java -jar yourapp.jar
bat file. Open again AlwaysUp
, click on the first button of the toolbar, as shown, select the Bat file written above and fill in the service name.
Once created, you can see the services we have configured in the list, and you can start the app in the background by right-clicking the selection Start xxx
.
Linux/unix
Let's talk about how to configure it on the server. In fact, there are many ways to implement it, and here are two more useful ways to do it:
Nohup and Shell
This method is implemented primarily by using nohup
commands, which are described in detail below:
Nohup command
Purpose: To run the command without hanging off.
Syntax: Nohup Command [Arg ...] [&]
Description: The nohup command runs commands specified by the command parameter and any related ARG parameters, ignoring all hang-up (SIGHUP) signals. Use the Nohup command to run a program in the background after logging off. To run the Nohup command in the background, add &
to the tail of the command.
So, we just need to use the nohup java -jar yourapp.jar &
command to get it yourapp.jar
running in the background. However, to facilitate management, we can also write some scripts to launch the application through the shell, such as the following:
- To close the app's script:
stop.sh
123456789 |
#!/bin/bash pid=$ (ps-ef | grep yourapp.jar | grep-v grep | awk ' {print $} ')if [-Z '$PID] then Echo application is already stopped else echo kill $PID kill $PID fi |
- To launch the app's script:
start.sh
12 |
#!/bin/bashnohup java-jar yourapp.jar--server.port=8888 & |
- Consolidated scripts for shutdown and startup: Because the app is closed before it is
run.sh
launched, it does not cause port conflicts and is suitable for repeated calls in a continuous integration system.
12345 |
#!/bin/bash Echo Stop Application Source stop.sh Echo Start Application Source start.sh |
System Services
In the spring boot maven plug-in, it also provides the ability to build a complete executable program, what does it mean? That is, java -jar
instead of running the jar directly, we can execute the program. This allows us to easily create a system service that runs in the background. The main steps are as follows:
pom.xml
Add a spring boot plug-in to the, and note the settings executable
configuration
1234567891011 |
<build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> <configuration> <executable>true</executable> </configuration> </plugin> </plugins> </build> |
After completing the above configuration, use mvn install
to package to build an executable jar package
Create a soft connection to the /etc/init.d/
directory
1 |
sudo ln -s/var/yourapp/yourapp.jar/etc/init.d/yourapp |
- Once the soft connection has been created, we can
yourapp.jar
control the start, stop, restart operation by using the following command
1 |
/etc/init.d/yourapp Start|stop|restart |
Background run configuration for Spring boot app