Javaアプリケーションを呼び出すUbuntu 16.04でサービスを開始、停止、および再起動するためにこのスクリプトを作成しました。 Ubuntuバージョンです
利用可能なLSBモジュールはありません。発行者ID:Ubuntu説明:Ubuntu 16.04.4 LTSバージョン:16.04コード名: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
サービスを開始するとコンソールにメッセージが表示されず、ステータスを確認するとサービスが開始および停止されるようです。
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$