[Java] - Run Java App as a Service in Cent OS

July 30, 2021 |

 


Run Java App as a Service in Cent OS

References:

  1. https://dzone.com/articles/run-your-java-application-as-a-service-on-ubuntu

Step 1: Create user

  • You should create a user for your service.

Ex:

$ groupadd group1

$ useradd user1 -M -s /bin/nologin -g gtest


Step 2: Create a service

$ vi /etc/systemd/system/your-service.service


Copy/past:

#!/bin/bash

[Unit]

Description=VCS Netty Service

[Service]

User=user1

# The configuration file application.properties should be here:


#change this to your workspace

WorkingDirectory=/home/user1


#path to executable.

#executable is a bash script which calls jar file

ExecStart=/home/user1/run-service


SuccessExitStatus=143

TimeoutStopSec=10

Restart=on-failure

RestartSec=5


[Install]

WantedBy=multi-user.target


Step 3: Create a Bash Script to Call Your Service

$ cd /home/user1

$ vi run-service.sh


#!/usr/bin/bash

/usr/bin/java -jar NETTY_SERVER-0.0.1-SNAPSHOT.jar


$ chmod u+x run-service.sh


Step 4: Start the Service

$ systemctl daemon-reload

$ systemctl enable your-service.service

$ systemctl start your-service

$ systemctl status your-service


Step 4: Check log

$ journalctl -f -u your-service


Read more…