Make your runnable jar service linux

To make your java a linux serivice.

You have to create a thread daemon inside your java code .

Generate a runnable jar file.

In the end put your jar in this script that can be call as a service.

(nohup — if you start a shell script or command and you exit (abort remote connection), the process / command will get killed. Sometime job or command takes a long time. If you are not sure when the job will finish, then it is better to leave job running in background. But, if you log out of the system, the job will be stopped and terminated by your shell.Nohup keep running your process.)

#!/bin/sh

SERVICE_NAME=UniitService

PATH_TO_JAR=/path your runnable jar.jar

PID_PATH_NAME=/path your –pid

case $1 in

    start)

        echo “Starting $SERVICE_NAME …

        if [ ! -f $PID_PATH_NAME ]; then

            nohup java -jar $PATH_TO_JAR  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  2>> /dev/null >> /dev/null & echo $! > $PID_PATH_NAME

            echo “$SERVICE_NAME started …

        else

            echo “$SERVICE_NAME is not running …

        fi

    ;;

esac