Configure SystemD for Nearby Shops

In this article we will learn how to automate the process execution using systemD.

SystemD is a mechanism in the Linux OS which automates the execution of the processes. It is required to ensure that the process does not terminate unexpectedly. When there is an unexpected termination of the process the systemD restarts the process. Since our API backend is suppose to run indefinitely unless we dont stop it by self. Therefore we need this mechanism to ensure that our process runs indefinitely.

A mechanism which monitors the running Java Process and restarts the process automatically when it is terminated unexpectedly. We also need to ensure that our process is started 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 the following articles

https://www.digitalocean.com/community/tutorials/how-to-use-systemctl-to-manage-systemd-services-and-units

https://www.digitalocean.com/community/tutorials/understanding-systemd-units-and-unit-files?utm_medium=btb_algo

https://www.digitalocean.com/community/tutorials/how-to-configure-a-linux-service-to-start-automatically-after-a-crash-or-reboot-part-1-practical-examples

After reading the above articles you will understand how SystemD works and what are unit files. 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 automate your process execution.

Prerequisite Knowledge

To understand this guide you dont need to be an expert but having some basic knowledge of linux terminal commands and java will help you.

Steps to configure SystemD

1. Navigate to /etc/systemd/system
2. Create a blank unit file
3. Add instructions in your unit file
4. Start the Service
5. Check whether the service is started
6. Enable the service to start automatically at Sytem Restart / Reboot
7. Check whether the service starts after a system Restart
8. Check whether service restarts at Unexpected Termination

Let us begin the step by step guide to configure SystemD

Step 1 : Navigate to /etc/systemd/system

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

                     Code
                  
    cd /etc/systemd/system
                    
                  

Step 2 : Create a blank unit file

                     Code
                  
    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.

                     Sample Unit File
                  

    [Unit]

    Description=nearbyshops_app: Nearbyshops Java API

    After=syslog.target network.target

    [Service]

    WorkingDirectory=/nearbyshops
    ExecStart=/usr/lib/jvm/java-8-openjdk-amd64/bin/java -jar  /nearbyshops/nearby-shops-api.jar
    Type=simple

    Restart=always

    [Install]

    WantedBy=multi-user.target

                    
                  

Copy and paste the contents of the above sample file in the new unit file we created in the previous step.

Please replace the filepath of jar file with the actual file path of your jar file.

Replace the working directory with the directory path in which your jar file exist.

Provide the full path to your java installation. This path may differ from the path given above.

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.

                     Code
                  
    sudo sytemctl daemon-reload
                    
                  

Start the service using the following command

                     Code
                  
    sudo systemctl start nearbyshopsapi.service
                    
                  

Step 5: Check whether the service is started

                     Code
                  
    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

                     Code
                  
    sudo systemctl enable nearbyshopsapi.service
                    
                  

this command will enable the service and this will allow the service to restart automatically at system reboot or just a normal 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

                     Code
                  
    sudo systemctl is-enabled nearbyshopsapi.service
                    
                  

Step 7 : Check whether the service starts after a system Restart

Type the following command to restart your Ubuntu server

                     Code
                  
    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.

                     Code
                  
    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 service restarts at Unexpected Termination

                     Code
                  
    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.

                     Code
                  
    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.

                     Code
                  
    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. Systemd Linux terminal Commands