This file encapsulates most common rc.d daemon script idioms, and should allow most of then to be about 6 lines long (not including whitespace) Using it will require some modifications to the /etc/conf.d files, as the generic functions expect all daemon options to be in $OPTS Among other things, this will close feature request# 18654 It was written on top of my bashification patch series, but is mostly independent of it. --- daemon-functions | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ functions | 2 + 2 files changed, 74 insertions(+), 0 deletions(-) diff --git a/daemon-functions b/daemon-functions new file mode 100755 index 0000000..5f7660a --- /dev/null +++ b/daemon-functions @@ -0,0 +1,72 @@ +#!/bin/bash +# This file contains helper functions for initscripts in /etc/rc.d. +# I am keeping it seperate from the main /etc/rc.d/functions for now. +# Using this file makes most /etc/rc.d files as simple as +# #!/bin/bash +# DAEMON=/path/to/daemon +# case $1 in +# start|stop|restart|status) $1;; +# *) usage;; +# esac + +shopt -s extglob + +# first, source our usual files. +. /etc/rc.conf +. /etc/rc.d/functions + +# Next, declare some useful helper functions +have_daemon() { + local f + [[ $DAEMON && -x $DAEMON ]] || return 1 + [[ $DAEMONNAME ]] || DAEMONNAME="${0##*/}" + PIDS=($(pidof -o %PPID "$DAEMON")) + [[ $DAEMONCONF ]] || \ + for f in "$DAEMONNAME" "$DAEMONNAME.conf"; do + [[ -f /etc/conf.d/$f ]] || continue + DAEMONCONF="/etc/conf.d/$f" + break + done + return 0 +} + +start() { + have_daemon || exit 1 + [[ -f $DAEMONCONF ]] && . "$DAEMONCONF" + stat_busy "Starting $DAEMONNAME" + if [[ ! $PIDS ]] && "$DAEMON" $OPTS >/dev/null 2>&1; then + add_daemon "$DAEMONNAME" + stat_done + else + stat_fail + fi +} + +stop() { + have_daemon || exit 1 + stat_busy "Stopping $DAEMONNAME" + if [[ $PIDS ]] && kill "${PIDS[@]}" >/dev/null 2>&1; then + rm_daemon "$DAEMONNAME" + stat_done + else + stat_fail + fi +} + +restart() { + have_daemon || exit 1 + stat_busy "Restarting $DAEMONNAME" + "$0" stop + sleep 1 + "$0" start +} + +status() { + have_daemon || exit 1 + stat_busy "Checking status of $DAEMONNAME" + ck_status "$DAEMONNAME" +} + +usage() { + echo "Usage: $0 {start|stop|restart|status}" +} \ No newline at end of file diff --git a/functions b/functions index 5e9a3b5..83f8fb7 100644 --- a/functions +++ b/functions @@ -197,8 +197,10 @@ status_stopped() { ck_status() { if ! ck_daemon "$1"; then status_started + return 0 else status_stopped + return 1 fi } -- 1.7.1.1