|
| 1 | +#!/bin/sh |
| 2 | +# See init-d-script(5) for instructions on how to use this library. |
| 3 | +#============================================================================= |
| 4 | +# Define LSB log_* functions. |
| 5 | +# Depend on lsb-base (>= 3.2-14) to ensure that this file is present |
| 6 | +# and status_of_proc is working. |
| 7 | +. /lib/lsb/init-functions |
| 8 | + |
| 9 | +# PATH should only include /usr/* if it runs after the mountnfs.sh |
| 10 | +# script. Scripts running before mountnfs.sh should remove the /usr/* |
| 11 | +# entries. |
| 12 | +PATH=/sbin:/usr/sbin:/bin:/usr/bin |
| 13 | +export PATH |
| 14 | + |
| 15 | +is_call_implemented() { |
| 16 | + command -V $1 > /dev/null 2>&1 |
| 17 | +} |
| 18 | + |
| 19 | +do_usage() { |
| 20 | + if is_call_implemented do_reload ; then |
| 21 | + echo "Usage: $SCRIPTNAME {start|stop|status|reload|restart|try-restart|force-reload}" >&2 |
| 22 | + else |
| 23 | + echo "Usage: $SCRIPTNAME {start|stop|status|restart|try-restart|force-reload}" >&2 |
| 24 | + fi |
| 25 | +} |
| 26 | + |
| 27 | +call() { |
| 28 | + cmd="$1" |
| 29 | + shift |
| 30 | + if is_call_implemented ${cmd}_override ; then |
| 31 | + ${cmd}_override "$@" |
| 32 | + else |
| 33 | + ${cmd} "$@" |
| 34 | + fi |
| 35 | +} |
| 36 | + |
| 37 | +# |
| 38 | +# Function that starts the daemon/service |
| 39 | +# |
| 40 | + |
| 41 | +# Return |
| 42 | +# 0 if daemon has been started |
| 43 | +# 1 if daemon was already running |
| 44 | +# 2 if daemon could not be started |
| 45 | +do_start_cmd() { |
| 46 | + start-stop-daemon --start --quiet ${PIDFILE:+--pidfile ${PIDFILE}} \ |
| 47 | + $START_ARGS \ |
| 48 | + --startas $DAEMON --name $NAME --exec $DAEMON --test > /dev/null \ |
| 49 | + || return 1 |
| 50 | + start-stop-daemon --start --quiet ${PIDFILE:+--pidfile ${PIDFILE}} \ |
| 51 | + $START_ARGS \ |
| 52 | + --startas $DAEMON --name $NAME --exec $DAEMON -- $DAEMON_ARGS \ |
| 53 | + || return 2 |
| 54 | + # Add code here, if necessary, that waits for the process to be ready |
| 55 | + # to handle requests from services started subsequently which depend |
| 56 | + # on this one. As a last resort, sleep for some time. |
| 57 | +} |
| 58 | + |
| 59 | +do_start() |
| 60 | +{ |
| 61 | + if is_call_implemented do_start_prepare ; then |
| 62 | + call do_start_prepare |
| 63 | + fi |
| 64 | + [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME" |
| 65 | + call do_start_cmd |
| 66 | + case "$?" in |
| 67 | + 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; |
| 68 | + 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; |
| 69 | + esac |
| 70 | + if is_call_implemented do_start_cleanup ; then |
| 71 | + call do_start_cleanup |
| 72 | + fi |
| 73 | +} |
| 74 | + |
| 75 | +# |
| 76 | +# Function that stops the daemon/service |
| 77 | +# |
| 78 | + |
| 79 | +# Return |
| 80 | +# 0 if daemon has been stopped |
| 81 | +# 1 if daemon was already stopped |
| 82 | +# 2 if daemon could not be stopped |
| 83 | +# other if a failure occurred |
| 84 | +do_stop_cmd() { |
| 85 | + start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 \ |
| 86 | + $STOP_ARGS \ |
| 87 | + ${PIDFILE:+--pidfile ${PIDFILE}} --name $NAME --exec $DAEMON |
| 88 | + RETVAL="$?" |
| 89 | + [ "$RETVAL" = 2 ] && return 2 |
| 90 | + # Wait for children to finish too if this is a daemon that forks |
| 91 | + # and if the daemon is only ever run from this initscript. |
| 92 | + # If the above conditions are not satisfied then add some other code |
| 93 | + # that waits for the process to drop all resources that could be |
| 94 | + # needed by services started subsequently. A last resort is to |
| 95 | + # sleep for some time. |
| 96 | + start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 \ |
| 97 | + $STOP_ARGS \ |
| 98 | + --exec $DAEMON |
| 99 | + [ "$?" = 2 ] && return 2 |
| 100 | + # Many daemons don't delete their pidfiles when they exit. |
| 101 | + rm -f $PIDFILE |
| 102 | + return $RETVAL |
| 103 | +} |
| 104 | + |
| 105 | +do_stop() |
| 106 | +{ |
| 107 | + if is_call_implemented do_stop_prepare ; then |
| 108 | + call do_stop_prepare |
| 109 | + fi |
| 110 | + [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME" |
| 111 | + call do_stop_cmd |
| 112 | + case "$?" in |
| 113 | + 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; |
| 114 | + 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; |
| 115 | + esac |
| 116 | + if is_call_implemented do_stop_cleanup ; then |
| 117 | + call do_stop_cleanup |
| 118 | + fi |
| 119 | +} |
| 120 | + |
| 121 | +do_restart() { |
| 122 | + [ "$VERBOSE" != no ] && log_daemon_msg "Restarting $DESC" "$NAME" |
| 123 | + call do_stop_cmd |
| 124 | + call do_start_cmd |
| 125 | + case "$?" in |
| 126 | + 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; |
| 127 | + 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; |
| 128 | + esac |
| 129 | +} |
| 130 | + |
| 131 | +do_force_reload() { |
| 132 | + if is_call_implemented do_reload ; then |
| 133 | + call do_reload |
| 134 | + else |
| 135 | + call do_restart |
| 136 | + fi |
| 137 | +} |
| 138 | + |
| 139 | +# Enable this using |
| 140 | +# alias do_reload=do_reload_sigusr1 |
| 141 | +do_reload_sigusr1() { |
| 142 | + log_daemon_msg "Reloading $DESC configuration files" "$NAME" |
| 143 | + start-stop-daemon --oknodo --stop --signal 1 --quiet \ |
| 144 | + --pidfile "$PIDFILE" --exec "$DAEMON" |
| 145 | + log_end_msg $? |
| 146 | +} |
| 147 | + |
| 148 | +do_status() { |
| 149 | + status_of_proc "$DAEMON" "$NAME" && return 0 || return $? |
| 150 | +} |
| 151 | + |
| 152 | +if [ "$DEBUG" = "true" ] ; then |
| 153 | + set -x |
| 154 | +fi |
| 155 | + |
| 156 | +SCRIPTNAME=$1 |
| 157 | +scriptbasename="$(basename $1)" |
| 158 | +if [ "$scriptbasename" != "init-d-script" ] ; then |
| 159 | + script="$1" |
| 160 | + shift |
| 161 | + . $script |
| 162 | +else |
| 163 | + exit 0 |
| 164 | +fi |
| 165 | + |
| 166 | +NAME=${NAME:=$(basename $DAEMON)} |
| 167 | +DESC=${DESC:=$NAME} |
| 168 | + |
| 169 | +# Do not use pid file if $PIDFILE is 'none'. Otherwise, generate from |
| 170 | +# $NAME or use the value provided by the init.d script. |
| 171 | +if [ none = "$PIDFILE" ] ; then |
| 172 | + PIDFILE= |
| 173 | +elif [ -z "$PIDFILE" ] ; then |
| 174 | + PIDFILE=/var/run/$NAME.pid |
| 175 | +fi |
| 176 | + |
| 177 | +# Exit if the package is not installed |
| 178 | +if [ none != "$DAEMON" ] && [ ! -x "$DAEMON" ] ; then |
| 179 | + exit 0 |
| 180 | +fi |
| 181 | + |
| 182 | +# Read configuration variable file if it is present |
| 183 | +[ -r /etc/default/$NAME ] && . /etc/default/$NAME |
| 184 | + |
| 185 | +# Load the VERBOSE setting and other rcS variables |
| 186 | +. /lib/init/vars.sh |
| 187 | +if [ -t 0 ] ; then # Be verbose when called from a terminal |
| 188 | + VERBOSE=yes |
| 189 | +fi |
| 190 | + |
| 191 | +case "$1" in |
| 192 | + start) |
| 193 | + call do_start |
| 194 | + ;; |
| 195 | + stop) |
| 196 | + call do_stop |
| 197 | + ;; |
| 198 | + status) |
| 199 | + call do_status |
| 200 | + ;; |
| 201 | + reload) |
| 202 | + if is_call_implemented do_reload ; then |
| 203 | + do_reload |
| 204 | + else |
| 205 | + call do_usage |
| 206 | + exit 3 |
| 207 | + fi |
| 208 | + ;; |
| 209 | + force-reload) |
| 210 | + call do_force_reload |
| 211 | + ;; |
| 212 | + restart) |
| 213 | + call do_restart |
| 214 | + ;; |
| 215 | + try-restart) |
| 216 | + log_daemon_msg "Trying to restart $DESC" "$NAME" |
| 217 | + if call do_status > /dev/null 2>&1 ; then |
| 218 | + call do_restart |
| 219 | + log_end_msg $? |
| 220 | + else |
| 221 | + log_progress_msg "is not running." |
| 222 | + log_end_msg 1 |
| 223 | + fi |
| 224 | + ;; |
| 225 | + '') |
| 226 | + call do_usage |
| 227 | + exit 3 |
| 228 | + ;; |
| 229 | + *) |
| 230 | + if is_call_implemented do_unknown ; then |
| 231 | + call do_unknown "$1" |
| 232 | + exit 3 |
| 233 | + else |
| 234 | + call do_usage |
| 235 | + exit 3 |
| 236 | + fi |
| 237 | + ;; |
| 238 | +esac |
| 239 | +exit 0 |
0 commit comments