Article updated on

Linux Java Service Wrapper Example

This is a simple wrapper to run a Java program as service. You need to be a root user.

Intructions:

  1. Create a file under  /etc/init.d/   with nano or vi and paste the example script below. ex.  sudo vi /etc/init.d/mytestserv
  2. 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.
  3. Write the file and give execution permisions ex. sudo chmod +x /etc/init.d/mytestserv
  4. Test that it runs ex. sudo service mytestserv start
  5. Test that it stops ex. sudo service mytestserv stop
  6. Test that it restarts ex. sudo service mytestserv restart

 

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&

 

Known Issues

  • Make sure your jar is not finishing execution by itself.
  • Use the nohup java -jar Myjar.jar & to verify that it can work as a service.
  • In Ubuntu use sudo update-rc.d mytestserv defaults if you want to run the service when the SO starts or sudo update-rc.d mytestserv disable to remove from startup.
  • In Ubuntu 16+ init.d is deprecated, you will get a MyService.service not found there is another tutorial with systemd here