How to write a linux service for running spring boot application?

The java spring boot application is the easiest and fastest way to start a web application is java world. The spring boot framework is almost plug and play able. Things are all setup inside spring boot and we just need to run it and immediately it works out of the box.

In some cases if we want to run the spring boot application as a linux service very easily we can set it up. The latest linux kernel has systemd where we can keep our services and it works like a demon.

When creating the linux service some of the parameters need to set properly. The user needs to be set correctly. In our case the user was: ubuntu.

Another important param is the working directory. If the spring boot has camel or other tools then this working directory has to be properly set otherwise it raises context related issues. In our case the WorkingDirectory was: var/www/<projectname>

The java bin path also needs to be set properly. In our case the java bin path was: /usr/lib/jvm/java-8-openjdk-amd64/bin/java

Following is our service that worked like a charm:

[Unit]
Description=<projectname>

[Service]
User=ubuntu
Group=ubuntu

# The configuration file application.properties should be here:
ExecStart=/usr/lib/jvm/java-8-openjdk-amd64/bin/java -jar  -Dspring.profiles.active=default /var/www/<projectname>/target/<projectname>-0.0.1-SNAPSHOT.jar
WorkingDirectory=<path to the project>

SuccessExitStatus=143
TimeoutStopSec=10
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target