Launch springboot project in the background on Linux

Posted by dannon on Mon, 20 Sep 2021 19:09:39 +0200

We know there are three ways to start a springboot project:

  • Run Main Method Start
  • Start the application on the command line using the command mvn spring-boot:run
  • When you run "mvn package" for packaging, it is packaged into a JAR file that can be run directly and run directly using the "java-jar" command.

We typically use the first two when developing and the third when deploying, but we don't run in the background when using java-jar.

Let's talk about how to get the springboot project started in the background on the server. In fact, there are many ways to do this, and here are two more useful ways:

nohup and Shell

This method is mainly implemented by using the nohup command, which is described in detail as follows:

nohup command

Purpose: Run commands without interruption.

Syntax: nohup Command [Arg...][&]

Description: The nohup command runs commands specified by Command parameters and any associated Arg parameters, ignoring all SIGHUP signals. Use the nohup command to run programs in the background after logoff. To run the nohup command in the background, add &to the end of the command.

Example:

nohup java -jar xxx.jar &

When this is done, nohup outputs the logs from the execution results to the nohup.out file in the current folder, usually using the above command.
We can also manually specify a parameter to specify where the log file will be output, such as:

nohup java -jar xxx.jar > catalina.out  2>&1 &

If you do not need to output a log, you can use the following command

nohup java -jar xxx.jar >/dev/null &

So we just need to use the nohup Java -jar yourapp.jar &command to get yourapp.jar running in the background. However, for easy administration, we can also write some scripts to start the application through Shell, such as the following:

  • Close the applied script: stop.sh
  • #!/bin/bash
    PID=$(ps -ef | grep yourapp.jar | grep -v grep | awk '{ print $2 }')
    if [ -z "$PID" ]
    then
        echo Application is already stopped
    else
        echo kill $PID
        kill $PID
    fi

    Script to start the application: start.sh

  • #!/bin/bash
    // Start via port 8888
    nohup java -jar yourapp.jar --server.port=8888 &
    
    #!/bin/bash    
    // Log output to yucoal.log with prod environment configuration parameters
    nohup java -jar yourapp.jar --spring.profiles.active=prod >/httx/logs/yuapp.log 2>&1 &

    Integrates shutdown and startup scripts: restart.sh, which is suitable for repeated calls in a continuous integration system because it executes the shutdown application before the application is started, without causing port conflicts.

  • #!/bin/bash
    echo stop application
    source stop.sh
    echo start application
    source start.sh

    system service

    In the Maven plug-in of Spring Boot, it also provides the ability to build a complete executable program. What does it mean? That is, we can run jar directly instead of java-jar to execute the program. In this way, we can easily create a system service to run in the background. The main steps are as follows:

  • Add Spring Boot plug-ins to pom.xml and note that executable configuration is set
  • <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 and build an executable jar package

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

sudo ln -s /var/yourapp/yourapp.jar /etc/init.d/yourapp

After the soft connection is created, you can control the start, stop, and restart operations by applying the following commands to yourapp.jar

/etc/init.d/yourapp start|stop|restart

In summary, jar packages can generally be started in the background using the following commands:

nohup java -jar xxx.jar &

Normally, we configure the log file ourselves when writing a java project. We don't need to output the default nohup.out log in a production environment, so we can start the jar package with the following command

nohup java -jar xxxx.jar >/dev/null 2>&1 &

From: Launch the springboot project in the background under linux - Say less - Blog Park

Topics: Java Linux Spring Boot