Eu criei este script para iniciar, parar e reiniciar um serviço no Ubuntu 16.04, o serviço irá chamar um aplicativo Java. Esta é a versão do Ubuntu
Nenhum módulo LSB está disponível. ID do Distribuidor: Ubuntu Descrição: Ubuntu 16.04.4 LTS Versão: 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
Quando inicio o serviço, nenhuma mensagem é exibida no console e, quando obtenho o status, parece que o serviço foi iniciado e interrompido:
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$