Auto restart Java Process after termination or System Reboot on Ubuntu 16.04 using SystemD

There are cases when we are running a Jar Process on our server and the process terminates unexpectedly. This process contains our Rest API therefore such unexpected termination of our service can shutdown our API Server. This is very sad because now people can no longer access and use your app.

The solution to this problem is the setup a mechanism which monitors the running Java Process and restarts the process automatically when its terminated unexpectedly. We will also set another mechanism which starts a process automatically on server restart.

We can solve this problem using SystemD init system which is available in Ubuntu 16.04.

You can learn more about SystemD init system by reading these articles


We will have to write a small script which manages and tells SystemD how to manage process execution for your process and configure it to restart your process when terminated.


Step 1 : Navigate to /etc/systemd/system

SystemD service unit files are located at this directory so we will cd into /etc/systemd/system

cd /etc/systemd/system


Step 2 : Create a blank unit file

sudo touch nearbyshopsapi.service

Type this command in your terminal. It will create a new file with name “nearbyshopsapi.service”. This is a blank file now and it does not have anything inside it. So in the next step we will open this file and add some instructions inside it.


Step 3 : Add instructions in your unit file

The following is an example of Sample Unit file.



Description=nearbyshops_app: Nearbyshops Java API


ExecStart=/usr/lib/jvm/java-8-openjdk-amd64/bin/java -jar     /nearbyshops/nbs_api_14feb17_2.jar




Copy and paste this example and replace the directory with the directory containing your jar file and also the replace the name of the jar file with the name of your jar file.


Step 4 : Start the Service 

Type the following command which reloads all the unit file. You need to type this command whenever you make any changes in the unit files.

  • sudo sytemctl daemon-reload

Start the service using the following command

sudo systemctl start nearbyshopsapi.service


Step 5: Check whether the service is started

sudo systemctl status nearbyshopsapi.service

this command will show you the status of the service and whether it is running or failed to run.

Step 6 :  Enable the service to start automatically at Sytem Restart / Reboot

sudo systemctl enabled nearbyshopsapi.service

this command will enable the service and this will allow the service to restart automatically at system Boot or system start. This will start the service automatically when the system is rebooted or restarted.

you can check whether you have successfully enabled the service or not by typing the following command

sudo systemctl is-enabled nearbyshopsapi.service


Step 7 : Check whether the service starts at system Restart

Type the following command to restart your Ubuntu server

sudo shutdown -r now

After the server is restarted and running. You need to check the status of the service to determine whether it is running or not.

sudo systemctl status nearbyshopsapi.service

Type the above command to check the status of the service. The service should be running.


Step 8 : Check whether the service restarts at Unexpected Termination

sudo systemctl status nearbyshopsapi.service

The above command will give you the process number. We will use this number to kill this process and check to see if the process restarts automatically or not.

sudo kill -9 process_number

Use the above command to kill the process.

Now check the status of the service by typing the following command. Is the Service Still Running ? If the answer is yes . This verifies that our mechanism is working successfully and the SystemD is monitoring and restarting the process automatically when terminated.

sudo systemctl status nearbyshopsapi.service


You will need to study various SystemD and systemCtl commands to manage things in a better way. For this purpose see this reference which lists various SystemCtl commands to manage the process execution.