0
votes

I am trying to run my jar as a service because my server should not shouting down when the system is getting shutdown. So I plan to start the server as a service.

My script is,

#!/bin/sh
### BEGIN INIT INFO
# Provides:          myservice
# Required-Start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# X-Interactive:     true
# Short-Description: Start/stop myservice server
### END INIT INFO

JARPATH="/usr/local/myservice/lib"
PID=$JARPATH/pid

start() {
    echo "Starting myservice ..."
    if [ ! -f $PID ]; then
        nohup java -jar $JARPATH/myservice-1.0.0.jar $JARPATH 2>> /dev/null >> /dev/null &
        echo $! > $PID
        echo "myservice started ..."
    else
        echo "myservice is already running ..."
    fi
}

stop() {
    if [ -f $PID ]; then
        #PID=$(cat /usr/local/myservice/pid);
        echo "Stopping myservice ..."
        kill $(cat "$PID");
        echo "myservice stopped ..."
        rm $PID
    else
        echo "myservice is not running ..."
    fi
}

case $1 in
    start)
        echo $JARPATH
        echo $PID
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    *)
esac

And I did the following steps,

  1. sudo chmod +x /etc/init.d/myservice
  2. sudo update-rc.d myservice defaults

And then start the service using the following command, But the above script is not working...

sudo service myservice start

Here what did I wrong?? Kindly provide your thoughts.

1
What is not working exactly ? - CCH
my server is not running... - SST
Is there any message when you sudo service myservice start ? - CCH
No there are no message.. - SST
please, try to use case "{$1}". Exmpl: case "${1} in "start") echo... ;; - Wild Fire

1 Answers

0
votes

The above script works fine when I put a double quotes in $1,

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    *)
esac