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 will dockerize later. For now they are plain java jar files generated by mvn. How do I use a python script or a Bash script to start them one by one and then use a script to terminate them. Is there some way where i start it and script won't block and the app will have a name that i can later use to stop the app?
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/myappOnce 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.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With