Deploying a spring boot application in Linux as system service

This is simple guide to building a jar and running the application in Linux based servers as system service.

Image Source

Please check this post to see how to bootstrap/create a simple project.

On spring-boot, we deploy/run jar or war file. Once you have the project, you can run ./gradlew clean bootJar command in the project to produce the jar file. The jar file will be located in folder — build/libs/ directory.

Once you have the jar built, you can copy the jar to server using scp command. Check this post to see how to copy file to server using scp.

In server, have java installed. I had used java8 to bootstrap the project and I have jdk8 installed in the server.

Place the jar in a directory in server. Ex: /home/user/apps/app1/spring-init-1.0.0.jar

Now, you can run the application using below command:

java -jar /home/user/apps/app1/spring-init-1.0.0.jar

The issue with running as above is it won’t run in background. Like if you close the running window, the app will also be stopped. To overcome that, you can create a systemd file. Systemd files are used to run the apps as system service in background.

sudo nano /etc/systemd/system/app1.service

Paste the below content to the file:

[Unit]
Description=Spring init sample
After=syslog.target

Save and close the file. Congrats!, you are done creating the service.

You can run below commands depending on what you need to do,

sudo systemctl start app1.service
sudo systemctl stop app1.service
sudo systemctl restart app1.service

Run below command to enable the service at startup, so that you don’t need to worry if your machine restarts. Once you enable, all the time when machine gets restarted, the app will start automatically.

sudo systemctl enable app1.service

Thanks for the read, send your feedback to connect@mevinod.com.

Start-up and tech enthusiast. I write about Tech, Devops and anything related.