我创建了这个脚本来启动、停止和重新启动 Ubuntu 16.04 中的服务,该服务将调用 Java 应用程序。这是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$