#! /bin/sh # # Copyright (c) 2018 Wind River Systems, Inc. # # SPDX-License-Identifier: Apache-2.0 # ### BEGIN INIT INFO # Provides: fm-api # Required-Start: $remote_fs $syslog # Required-Stop: $remote_fs $syslog # Default-Start: 3 5 # Default-Stop: 0 1 2 6 # Short-Description: Fault Management REST API Service # Description: Fault Management REST API Service ### END INIT INFO . /etc/init.d/functions # Linux Standard Base (LSB) Error Codes RETVAL=0 GENERIC_ERROR=1 INVALID_ARGS=2 UNSUPPORTED_FEATURE=3 NOT_INSTALLED=5 NOT_RUNNING=7 NAME="fm-api" DAEMON="/usr/bin/${NAME}" PIDFILE="/var/run/${NAME}.pid" CONFIGFILE="/etc/fm/fm.conf" if ! [ -x ${DAEMON} ] ; then logger "${DAEMON} is missing" exit ${NOT_INSTALLED} fi PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin export PATH status() { pid=`cat $PIDFILE 2>/dev/null` if [ -n "$pid" ]; then if ps -p $pid | grep $NAME &> /dev/null ; then echo "$NAME is running" return 0 fi fi echo "$NAME is not running" return 1 } start () { status >/dev/null if [ $? -eq 0 ]; then echo "$NAME is already running" return 0 fi # Delete stale pidfile, if any rm -f $PIDFILE start-stop-daemon --start -b --make-pidfile --pidfile $PIDFILE -x ${DAEMON} -- --config-file=${CONFIGFILE} RETVAL=$? if [ ${RETVAL} -eq 0 ]; then status >/dev/null if [ $? -eq 0 ]; then logger -t $NAME "start OK" echo "OK" return 0 fi logger -t $NAME "start-stop-daemon returned 0, but status fails" rm -f $PIDFILE fi logger -t $NAME "start failed" return ${GENERIC_ERROR} } confirm_stop() { local my_processes=`pgrep -l -f "^(python|/usr/bin/python|/usr/bin/python2|/usr/bin/python3|/usr/libexec/platform-python) ${DAEMON}([^\w-]|$)"` if [ -n "${my_processes}" ] then logger -t $NAME "About to SIGKILL the following: ${my_processes}" pkill -KILL -f "^(python|/usr/bin/python|/usr/bin/python2|/usr/bin/python3|/usr/libexec/platform-python) ${DAEMON}([^\w-]|$)" fi } stop () { status >/dev/null if [ $? -ne 0 ]; then echo "$NAME is not running" return 0 fi echo -n "Stopping ${NAME}: " if [ -f $PIDFILE ]; then start-stop-daemon --stop --quiet --retry 3 --oknodo --pidfile $PIDFILE fi confirm_stop rm -f $PIDFILE # Confirm status status >/dev/null if [ $? -ne 0 ]; then echo "Stopped" return 0 else echo "Failed" return ${GENERIC_ERROR} fi } rc=0 case "$1" in start) start rc=$? ;; stop) stop rc=$? ;; restart|force-reload|reload) stop start rc=$? ;; status) status rc=$? ;; *) echo "Usage: $0 {start|stop|force-reload|restart|reload|status}" exit 1 ;; esac exit $rc