Article updated on

Ubuntu 16+ Java Service Wrapper Example for Systemd

This is a simple wrapper to run a Java program as service. You need to be a root user. In this example SYSTEMD is used instead of init.d because it's becoming obsolete. For older systems click here

Instructions:

  1. Create a file under  /etc/systemd/system/   with nano or vi and paste the example script below. eg.  sudo vi /etc/systemd/system/MyService.service
  2. Paste the code below in your new file:

[Unit]
Description = Java Service
After network.target = MyService.service

[Service]
Type = forking
ExecStart = /usr/local/bin/MyService.sh start
ExecStop = /usr/local/bin/MyService.sh stop
ExecReload = /usr/local/bin/MyService.sh reload

[Install]
WantedBy=multi-user.target

  1. Create a file with under /usr/local/bin/ eg. sudo vi /usr/local/bin/MyService.sh
  2. Paste de Code example below
  3. Modify the SERVICE_NAME, PATH_TO_JAR, and choose a PID_PATH_NAME for the file you are going to use to store your service ID.
  4. Write the file and give execution permisions ex. sudo chmod +x /usr/local/bin/MyService.sh
  5. Test that it runs ex. /usr/local/bin/./MyService.sh start
  6. Test that it stops ex. /usr/local/bin/./MyService.sh stop
  7. Test that it restarts ex. /usr/local/bin/./MyService.sh restart
  8. Enable the service with the command sudo systemctl enable MyService
  9. To run the service sudo systemctl start MyService.service
  10. To stop the service sudo systemctl stop MyService.service

Copy the code example:

#!/bin/sh
SERVICE_NAME=MyService
PATH_TO_JAR=/usr/local/MyProject/MyJar.jar
PID_PATH_NAME=/tmp/MyService-pid
case $1 in
    start)
        echo "Starting $SERVICE_NAME ..."
        if [ ! -f $PID_PATH_NAME ]; then
            nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
                        echo $! > $PID_PATH_NAME
            echo "$SERVICE_NAME started ..."
        else
            echo "$SERVICE_NAME is already running ..."
        fi
    ;;
    stop)
        if [ -f $PID_PATH_NAME ]; then
            PID=$(cat $PID_PATH_NAME);
            echo "$SERVICE_NAME stoping ..."
            kill $PID;
            echo "$SERVICE_NAME stopped ..."
            rm $PID_PATH_NAME
        else
            echo "$SERVICE_NAME is not running ..."
        fi
    ;;
    restart)
        if [ -f $PID_PATH_NAME ]; then
            PID=$(cat $PID_PATH_NAME);
            echo "$SERVICE_NAME stopping ...";
            kill $PID;
            echo "$SERVICE_NAME stopped ...";
            rm $PID_PATH_NAME
            echo "$SERVICE_NAME starting ..."
            nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
                        echo $! > $PID_PATH_NAME
            echo "$SERVICE_NAME started ..."
        else
            echo "$SERVICE_NAME is not running ..."
        fi
    ;;
esac
 

 

Modify if you need logs

If you need the output log replace the 2

nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &

 

lines for:

nohup java -jar $PATH_TO_JAR >> myService.out 2>&1&

 

 

Notes

  • Reference here
  • Tested with 16.04.1-Ubuntu SMP