service helper package
is there something like a service-common package that provides a helper script like the following for services to source?
I'm thinking that it would belong somewhere like /usr/share/service-common/init.sh. I have not tested the following yet, and I'm a sucky bash programmer.
# Fully qualified paths to required programs
START_STOP_DAEMON=/sbin/start-stop-daemon
CAT=/bin/cat
ECHO=/bin/echo
# Ensure that all required programs exist on the system
for REQUIRED_PROGRAM in $START_STOP_DAEMON $CAT $ECHO ; do
if [ ! -f $REQUIRED_PROGRAM ]; then
fatal( "Required program, '$REQUIRED_PROGRAM' does not exist." )
fi
done
if [ -z "$SERVICE_NAME" ] || \
[ -z "$SERVICE_DESC" ] || \
[ -z "$SERVICE_DAEMON
" ]; then
fatal( "Environment not configured correctly.\n\tService requires definition of the following variables:\n
SERVICE_NAME\nSERVICE_DESC\nSERVICE_DAEMON
" )
fi
# We do not want to be affected by PATH tampering. All calls to
# external programs will be fully qualified
PATH=""
SERVICE_RUNDIR=/var/run/$SERVICE_NAME
SERVICE_PIDFILE=$SERVICE_RUNDIR/$SERVICE_NAME.pid
SERVICE_CONFDIR=/etc/$SERVICE_NAME
SERVICE_SENTINEL_FILE=$SERVICE_CONFDIR/disable
SERVICE_ENABLED=yes
SERVICE_PARAMS=""
SERVICE_INIT_SCRIPT=/etc/init.d/$SERVICE_NAME
# Source service-specific default values
. /etc/default/$SERVICE_NAME
# Kill me on all errors
set -e
# If the daemon binary does not exist, report the error and exit
if [ ! -f $SERVICE_DAEMON ]; then
fatal( "Service daemon, '$SERVICE_DAEMON' does not exist." )
fi
# Make sure the RUNDIR exists with correct permissions
if [ ! -d "$RUNDIR" ]; then
mkdir -p "$RUNDIR"
# If a service user is not zero length, chown the rundir
if [ -n "$SERVICE_USER" ]; then
chown -R "$SERVICE_USER" "$RUNDIR"
fi
# If a service group is not zero length, chown the rundir
if [ -n "$SERVICE_GROUP" ]; then
chgrp -R "$SERVICE_GROUP" "$RUNDIR"
fi
# Only allow $RUNDIR to be read/written/entered by root or service
# user/group
chmod 770 "$RUNDIR"
fi
# main switch statement - process service state change request
case "$1" in
start)
start_service()
;;
stop)
stop_service()
;;
reload)
reload_service()
;;
restart)
restart_service()
;;
force-reload)
if [ "x$SERVICE_GRACEFUL_RELOAD" ]; then
reload_service()
else
restart_service()
fi
;;
*)
echo "Usage: $INIT_SCRIPT {start|stop|restart|reload|force-reload}" >&2
exit 1
;;
esac
exit 0
# echo an error message and quit
fatal() {
echo " - failed: "
if [ -z "$1" ]; then
echo "$1"
else
$CAT <<EOF
The operation failed but no output was produced. For hints on what went
wrong please refer to the system's logfiles (e.g. /var/log/syslog) or
try running the daemon in Debug mode like via "$SERVICE_DAEMON
$SERVICE_DEBUG_ARGS" (warning: this may create copious output).
EOF
fi
exit 1
}
# Check whether we were configured to not start the services.
check_for_no_start() {
if [ "$SERVICE_DISABLED" = "yes" ]; then
echo "Not starting $SERVICE_NAME: SERVICE_DISABLED set in /etc/default/$SERVICE_NAME" >&2
exit 0
fi
if [ -n "$SERVICE_SENTINEL_FILE" ] && [ -e "$SERVICE_SENTINEL_FILE" ]; then
echo "Not starting $SERVICE_NAME: $SERVICE_SENTINEL_FILE exists" >&2
exit 0
fi
}
start_service() {
check_for_no_start();
echo -n "Starting $SERVICE_DESC: "
$START_STOP_DAEMON \
--start \
--quiet \
--background \
--make-pidfile \
--pidfile $SERVICE_PIDFILE \
--chdir $SERVICE_RUNDIR \
--exec $SERVICE_DAEMON -- $SERVICE_PARAMS
echo "$SERVICE_NAME."
}
stop_service() {
echo -n "Stopping $SERVICE_DESC: "
if [ ! -f $SERVICE_PIDFILE ]; then
echo "not running."
exit 0
fi
$START_STOP_DAEMON \
--stop \
--oknodo \
--quiet \
--pidfile $SERVICE_PIDFILE \
--exec $SERVICE_DAEMON -- $SERVICE_PARAMS
echo "$SERVICE_NAME."
}
reload_service() {
# If the service does not support graceful reload, tell the user
# and fail
if [ "x$SERVICE_GRACEFUL_RELOAD" = "xno" ]; then
fatal( "Service does not support graceful reload. Try 'restart' instead." )
fi
# If the daemon responds to changes in its config file directly
# anyway, make this a do-nothing entry.
if [ "x$SERVICE_AUTO_REREADS_CONFIG" = "yes" ]; then
echo "Service '$SERVICE_NAME' reloads configuration automatically."
exit 0
fi
# If the daemon can reload its config files on the fly for example
# by sending it SIGHUP, do it here.
echo -n "Sending SIGHUP to $SERVICE_DESC: "
$START_STOP_DAEMON \
--stop \
--signal 1 \
--quiet \
--pidfile $SERVICE_PIDFILE \
--exec $SERVICE_DAEMON
echo "$SERVICE_NAME."
}
restart_service() {
stop_service()
sleep 1
start_service()
}
--
moo.
Reply to: