creating service from executing a JAR file in UBUNTU

1.1k Views Asked by At

I'm trying to create a service from executing a jar file in Ubuntu machine. I have created a wrapper with the below code

#!/bin/sh
SERVICE_NAME=MyService
PATH_TO_JAR=/home/nani/Documents/My_Build_executable/My_Build-12-06.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

but I'm ge stuck with the PID_PATH_NAME. what is this PID and what exactly I need to do to configure/create/modify this one? please help me.

1

There are 1 best solutions below

7
On

The term PID stands for Process IDentifier, and is a unique number each process gets. By saving the PID to a file, the system can later find the PID of the service, and will be able to, for example, send a stop-signal to the service when it should be stopped.