Background run configuration of the JAR (Spring Boot) app

Source: Internet
Author: User

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 the jar package, run with Java-jar

When we develop, we usually use the first two, but we often use a third when we deploy. However, when we use Java-jar to run, it is not running in the background. 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 mvn install the Spring boot app into a jar package, and then write a Java-jar yourapp.jar bat file. Then open 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 configured in the list, and by right-clicking on start XXX You can start the app in the background.

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 mainly implemented by using the Nohup command, which is 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 make Yourapp.jar run 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
#!/Bin/BashPid=$(Ps-Ef|grep yourapp.| grep -v grep | awk  ' {print $2  } ' ) if [-z Span class= "s" > "$PID" ]then echo application is already stoppedelse echo kill  $PID kill  $PID fi    
    • Script to launch the app: start.sh
#!/bin/bashnohup java -jar yourapp.jar --server.port=8888 &
    • Integration of the shutdown and startup scripts: Run.sh, because the application will be closed first, and then start the application, which will not cause port conflicts and other problems, suitable for repeated calls in the continuous integration System.
#!/bin/bashecho stop applicationsource stop.shecho start applicationsource 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, instead of Java-jar, we can run the jar directly to execute the program. This allows us to easily create a system service that runs in the background. The main steps are as follows:

    • Add the Spring boot plug-in to Pom.xml and note that the executable configuration is set
<Build><Plugins><Plugin><GroupId>Org.Springframework.Boot</GroupId><artifactid>spring-boot -maven-plugin</ artifactid> <configuration > <executable>true< Span class= "o" ></executable> </ Configuration> </plugin> </plugins> </build< Span class= "O" >>               
    • After completing the above configuration, use MVN install to package, build an executable jar package

    • Create a soft connection to the/etc/init.d/directory

sudo ln -s /var/yourapp/yourapp.jar /etc/init.d/yourapp
    • After the soft connection is created, we can control the start, stop, restart operation with the following command for the Yourapp.jar application
/etc/init.d/yourapp start|stop|restart

Background run configuration of the JAR (Spring Boot) app

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.