How to configure nicely Spring Boot application packaged as executable jar as a Service in linux system? Is this recommended approach, or should I convert this app to war and install into Tomcat?
Currently I can run Spring boot application from screen
session, what is nice, but requires manual start after server reboot.
What I'm looking for is general advice/direction or sample init.d
script, if my approach with executable jar is proper.
AS A WINDOWS SERVICE
If you want this to run in windows machine download the winsw.exe from
After that rename it to jar filename (eg: your-app.jar)
Now create an xml file your-app.xml and copy the following content to that
Make sure that the exe and xml along with jar in a same folder.
After this open command prompt in Administrator previlege and install it to the windows service.
If it fails with
Then try the following:
thats it :) .
To uninstall the service in windows
For see/run/stop service: win+r and type Administrative tools then select the service from that. Then right click choose the option - run / stop
I just got around to doing this myself, so the following is where I am so far in terms of a CentOS init.d service controller script. It's working quite nicely so far, but I'm no leet Bash hacker, so I'm sure there's room for improvement, so thoughts on improving it are welcome.
First of all, I have a short config script
/data/svcmgmt/conf/my-spring-boot-api.sh
for each service, which sets up environment variables.I'm using CentOS, so to ensure that my services are started after a server restart, I have a service control script in
/etc/init.d/my-spring-boot-api
:As you can see, that calls the initial config script to set up environment variables and then calls a shared script which I use for restarting all of my Spring Boot services. That shared script is where the meat of it all can be found:
When stopping, it will attempt to use Spring Boot Actuator to perform a controlled shutdown. However, in case Actuator is not configured or fails to shut down within a reasonable time frame (I give it 5 seconds, which is a bit short really), the process will be killed.
Also, the script makes the assumption that the java process running the appllication will be the only one with "my-spring-boot-api.jar" in the text of the process details. This is a safe assumption in my environment and means that I don't need to keep track of PIDs.
The following works for springboot 1.3 and above:
As init.d service
The executable jar has the usual start, stop, restart, and status commands. It will also set up a PID file in the usual /var/run directory and logging in the usual /var/log directory by default.
You just need to symlink your jar into /etc/init.d like so
OR
After that you can do the usual
Then setup a link in whichever runlevel you want the app to start/stop in on boot if so desired.
As a systemd service
To run a Spring Boot application installed in var/myapp you can add the following script in /etc/systemd/system/myapp.service:
Reference
http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/html/deployment-install.html#deployment-service
Following up on Chad's excellent answer, if you get an error of "Error: Could not find or load main class" - and you spend a couple hours trying to troubleshoot it, whether your executing a shell script that starts your java app or starting it from systemd itself - and you know your classpath is 100% correct, e.g. manually running the shell script works as well as running what you have in systemd execstart. Be sure you're running things as the correct user! In my case, I had tried different users, after quite a while of troubleshooting - i finally had a hunch, put root as the user - voila, the app started correctly. After determining it was a wrong user issue, I
chown -R user:user
the folder and subfolders and the app ran correctly as the specified user and group so no longer needed to run it as root (bad security).I am trying to make springboot applications that are presented as a "init.d" style shell script with a compressed java application tacked on the end
By symlinking these scripts from /etc/init.d/spring-app to /opt/spring-app.jar and chmod'ing the jar to be executable it is possible to make "/etc/init.d/spring-app start" "/etc/init.d/spring-app stop" and other possibilities like status work
Presumably, as the init.d style scripts from springboot look that they have the neccessary magic strings ( like
# Default-Start: 2 3 4 5
) chkconfig would be able to add it as a "service"But I wanted to get it to work with systemd
To make this work I tried many of the recipies in the other answers above but none of them worked for me on Centos 7.2 with Springboot 1.3 Mostly they would start the service but not be able to track the pid
In the end I found the following did work for me, when the /etc/init.d link was in place also. A file similar to the one below should be installed as
/usr/lib/systemd/system/spring-app.service
My SysVInit script for Centos 6 / RHEL (not ideal yet). This script requires ApplicationPidListener.
Source of
/etc/init.d/app
Sample config file
/etc/sysconfig/app
: