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.
If you want to use Spring Boot 1.2.5 with Spring Boot Maven Plugin 1.3.0.M2, here's out solution:
Then compile as ususal:
mvn clean package
, make a symlinkln -s /.../myapp.jar /etc/init.d/myapp
, make it executablechmod +x /etc/init.d/myapp
and start itservice myapp start
(with Ubuntu Server)I don't know of a "standard" shrink-wrapped way to do that with a Java app, but it's definitely a good idea (you want to benefit from the keep-alive and monitoring capabilities of the operating system if they are there). It's on the roadmap to provide something from the Spring Boot tool support (maven and gradle), but for now you are probably going to have to roll your own. The best solution I know of right now is Foreman, which has a declarative approach and one line commands for packaging init scripts for various standard OS formats (monit, sys V, upstart etc.). There is also evidence of people having set stuff up with gradle (e.g. here).
Here is a script that deploys an executable jar as a systemd service.
It creates a user for the service and the .service file, and place the jar file under /var, and makes some basic lock down of privileges.
Example:
What follows is the easiest way to install a Java application as system service in Linux.
Let's assume you are using
systemd
(which any modern distro nowadays does):Firstly, create a service file in
/etc/systemd/system
named e.g.javaservice.service
with this content:Secondly, notify
systemd
of the new service file:and enable it, so it runs on boot:
Eventually, you can use the following commands to start/stop your new service:
Provided you are using
systemd
, this is the most non-intrusive and clean way to set up a Java application as system-service.What I like especially about this solution is the fact that you don't need to install and configure any other software. The shipped
systemd
does all the work for you, and your service behaves like any other system service. I use it in production for a while now, on different distros, and it just works as you would expect.Another plus is that, by using
/usr/bin/java
, you can easily addjvm
paramters such as-Xmx256m
.Also read the
systemd
part in the official Spring Boot documentation: http://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.htmlYou could also use supervisord which is a very handy daemon, which can be used to easily control services. These services are defined by simple configuration files defining what to execute with which user in which directory and so forth, there are a zillion options. supervisord has a very simple syntax, so it makes a very good alternative to writing SysV init scripts.
Here a simple supervisord configuration file for the program you are trying to run/control. (put this into /etc/supervisor/conf.d/yourapp.conf)
/etc/supervisor/conf.d/yourapp.conf
To control the application you would need to execute supervisorctl, which will present you with a prompt where you could start, stop, status yourapp.
CLI
If the
supervisord
daemon is already running and you've added the configuration for your serivce without restarting the daemon you can simply do areread
andupdate
command in thesupervisorctl
shell.This really gives you all the flexibilites you would have using SysV Init scripts, but easy to use and control. Take a look at the documentation.
In this question, the answer from @PbxMan should get you started:
Run a Java Application as a Service on Linux
Edit:
There is another, less nice way to start a process on reboot, using cron:
This works, but gives you no nice start/stop interface for your application. You can still simply
kill
it anyway...