Run your Java application as a Service on Ubuntu

Sulman Sarwar
2 min readSep 18, 2017

You have a jar file and you need to run it as a service. Additionally, you want it to start automatically if/when system restarts.

Ubuntu has built in mechanism to create custom services, enable them to get started at system boot time and start/stop them as a service. In this post I am going to share a simple and elegant way to create a service wrapper for your jar file and run it as a service. Here we go:

Step 1 — Create a service

 sudo vim /etc/systemd/system/my-webapp.service 

copy/paste following into the file /etc/systemd/system/my-webapp.service

[Unit]
Description=My Webapp Java REST Service
[Service]
User=ubuntu
# The configuration file application.properties should be here:
#change this to your workspace
WorkingDirectory=/home/ubuntu/workspace
#path to executable.
#executable is a bash script which calls jar file ExecStart=/home/ubuntu/workspace/my-webapp
SuccessExitStatus=143
TimeoutStopSec=10
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target

Step 2 — Create bash script to call your service

--

--