Starting Spring boot applications from script

前端 未结 4 1005
醉梦人生
醉梦人生 2021-01-17 04:28

Using normal spring mvn commands, I can start a spring boot application from command line and terminate it with Control+c. I however have created a bunch of services which I

相关标签:
4条回答
  • 2021-01-17 04:58

    You could use a script to achieve this. For example a startup.sh may look like this. It will start the application and write the process id to /path/to/app/pid.file

    #!/bin/bash
    nohup java -jar /path/to/app/hello-world.jar > /path/to/log.txt 2>&1 &
    echo $! > /path/to/app/pid.file
    

    And a shutdown.sh may look like this.

    #!/bin/bash
    kill $(cat /path/to/app/pid.file)
    

    You can find more detail in my post. https://springhow.com/start-stop-scripts-for-spring-boot-applications/

    0 讨论(0)
  • 2021-01-17 05:05

    This script make it easy, auto find newest version of jar file:

    https://github.com/tyrion9/spring-boot-startup-script

    ./bootstrap.sh start
    ./bootstrap.sh stop
    ./bootstrap.sh restart
    
    0 讨论(0)
  • 2021-01-17 05:15

    You can launch each jar with the following command (in a bash script):

    java -jar service1.jar &
    

    Then, you can kill each process with the following command (in a bash script):

    pkill -f service1.jar
    

    pkill will terminates all processes containing the provided name. Be careful that your keyword only identifies your process, so you don't terminate other processes by mistake.

    0 讨论(0)
  • 2021-01-17 05:16

    I would follow the documentation to install Spring-Boot application as a Unix/Linux service.

    All you have to do is to add this dependency to your pom.xml:

    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <executable>true</executable>
        </configuration>
    </plugin>
    

    After adding the plugin you should install and create a symlink to your application (exact part of documentation):

    Assuming that you have a Spring Boot application installed in /var/myapp, to install a Spring Boot application as an init.d service simply create a symlink:

    $ sudo ln -s /var/myapp/myapp.jar /etc/init.d/myapp

    Once installed, you can start and stop the service in the usual way. For example, on a Debian based system:

    $ service myapp start

    Then you are able to create a bash script to start, stop or restart your applications in a clean way.

    0 讨论(0)
提交回复
热议问题