1

I've created this script to start, stop and restart a service in Ubuntu 16.04, the service will call a Java application. This is the Ubuntu version

No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 16.04.4 LTS Release: 16.04 Codename: xenial

#!/bin/bash
# menu-core-prices-update daemon
# description: daemon for the Java app. that will update the ccy prices every 5 min.
# processname: menu-core-prices-update

DAEMON_PATH="/opt/menu/core-price-update"
DAEMON="java -jar menu-core-prices-update-0.0.1-SNAPSHOT.jar"
DAEMONOPTS=""
NAME=menu-core-prices-update
DESC="menu Core Prices daemon. Java process."
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

case "$1" in
start)
    printf "%-50s" "Starting $NAME..."
    cd $DAEMON_PATH
    PID=`$DAEMON $DAEMONOPTS > /dev/null 2>&1 & echo $!`
    #echo "Saving PID" $PID " to " $PIDFILE
        if [ -z $PID ]; then
            printf "%s\n" "Fail"
        else
            echo $PID > $PIDFILE
            printf "%s\n" "Ok"
        fi
;;
status)
        printf "%-50s" "Checking $NAME..."
        if [ -f $PIDFILE ]; then
            PID=`cat $PIDFILE`
            if [ -z "`ps axf | grep ${PID} | grep -v grep`" ]; then
                printf "%s\n" "Process dead but pidfile exists"
            else
                echo "Running"
            fi
        else
            printf "%s\n" "Service not running"
        fi
;;
stop)
        printf "%-50s" "Stopping $NAME"
            PID=`cat $PIDFILE`
            cd $DAEMON_PATH
        if [ -f $PIDFILE ]; then
            kill -HUP $PID
            printf "%s\n" "Ok"
            rm -f $PIDFILE
        else
            printf "%s\n" "pidfile not found"
        fi
;;
restart)
    $0 stop
    sleep 1
    $0 start
;;
*)
        echo "Usage: $0 {status|start|stop|restart}"
        exit 1
esac

When I start the service no message is prompted on the console, and when I get the status it seems that the service started and stopped:

canperis@localhost:/usr/local/bin$ sudo systemctl start menu-core-prices-update.service
canperis@localhost:/usr/local/bin$ sudo systemctl status menu-core-prices-update.service
● menu-core-prices-update.service - core-price-update daemon
   Loaded: loaded (/lib/systemd/system/menu-core-prices-update.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Wed 2018-07-25 12:11:02 UTC; 10s ago
  Process: 4482 ExecStop=/usr/local/bin/menu-core-prices-update.sh stop (code=exited, status=0/SUCCESS)
  Process: 4479 ExecStart=/usr/local/bin/menu-core-prices-update.sh start (code=exited, status=0/SUCCESS)
 Main PID: 4479 (code=exited, status=0/SUCCESS)

Jul 25 12:11:02 localhost systemd[1]: Started core-price-update daemon.
Jul 25 12:11:02 localhost menu-core-prices-update.sh[4479]: Starting menu-core-prices-update...            Ok
Jul 25 12:11:02 localhost menu-core-prices-update.sh[4482]: Stopping menu-core-prices-update...            Ok
canperis@localhost:/usr/local/bin$ 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.