1

I've generated a Spring Boot app. with maven, moved the jar into

/var/licence/licence-0.0.1-SNAPSHOT.jar

Then

sudo ln -s /var/licence/licence-0.0.1-SNAPSHOT.jar /etc/init.d/licence

But when I type

lopes@localhost:/var/licence$ service licence start
licence: unrecognized service

These are the attributes of /etc/init.d/licence

lrwxrwxrwx   1 root root   46 Mar 27 18:17 licence -> /var/licence/licence-0.0.1-SNAPSHOT.jar

This is my pom.xml:

....

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>   
                <configuration>
                    <executable>true</executable>
                </configuration>            
            </plugin>
        </plugins>
    </build>
...
en Lopes
  • 1,863
  • 11
  • 48
  • 90
  • Did you create an executable jar as explained in the reference guide? – M. Deinum Mar 27 '17 at 18:48
  • yes, I created an executable jar as explained in the reference guid – en Lopes Mar 27 '17 at 19:29
  • Which boot version are you using and have you checked the logs? As mentioned [here](https://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html#deployment-initd-service)? – M. Deinum Mar 28 '17 at 05:43

2 Answers2

5

As I know /etc/init.d stores only script files. You should put there script file, where you describe how to start/stop your service. Below I wrote sample script save it as /etc/init.d/license and you can start/stop your service like this sudo service license start

sample scriptdetail in this resource

#!/bin/sh
SERVICE_NAME=License
PATH_TO_JAR=/var/licence/licence-0.0.1-SNAPSHOT.jar
PID_PATH_NAME=/tmp/License.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 
DanikX
  • 101
  • 1
  • 6
  • 2
    An executable Spring Boot jar is a script as explained [here](https://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html#deployment-initd-service). – M. Deinum Mar 28 '17 at 05:44
3

Instead of the old-fashioned /etc/init.d you'd better use a systemd unit file. systemd is the init system used by most Linux distros, and integrating your service is both easier (see below) and more powerful because you get for free supervisioning, logging facilities, dependency management on other services and so on.

Regarding an example for your service, you first need to put a license.service in the directory /etc/systemd/system with contents like:

[Unit]
Description=License service by MyCorp

[Service]
ExecStart=/var/licence/licence-0.0.1-SNAPSHOT.jar
User=myuser
Restart=on-failure

[Install]
WantedBy=default.target

and then run these commands:

#>systemctl daemon-reload
#>systemctl enable license
#>systemctl start license

Note that SpringBoot can create a executable JAR (ie a JAR that starts with a shell script) so you don't need to call java explicitely. See the spring.io documentation for how to customize.

Raffaele
  • 20,627
  • 6
  • 47
  • 86
  • 1
    You shouldn't need the `java -jar` part as the `jar` should be the executable (script) as explained [here](https://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html#deployment-systemd-service). – M. Deinum Mar 28 '17 at 05:45
  • Thanks. Updated the answer – Raffaele Mar 28 '17 at 17:23