3 # quagga Starts/stops quagga daemons and watchquagga.
4 # Create a daemon.conf file to have that routing daemon
5 # started/stopped automagically when using this script
6 # without any daemon names as args.
7 # If watchquagga is available, it will also be
8 # started/stopped if the script is called without
15 echo "Usage: ${ME} {start|stop|restart} [daemon ...]"
29 STATEDIR
=/var
/run
/quagga
30 DAEMONS
="zebra ripd ripngd ospfd ospf6d bgpd"
32 WATCHQUAGGA_FLAGS
="-d -z -T 60 -R"
33 WATCHQUAGGA_CMD
="$0 watchrestart"
34 if [ ${COMMAND} != "watchrestart" ]
36 DAEMONS
="${DAEMONS} watchquagga"
38 DAEMONS_STARTSEQ
=${DAEMONS}
51 DAEMONS_STOPSEQ
=$
(reverse
${DAEMONS_STARTSEQ})
54 # ps ax | awk 'match($5, "(^|/)'"$1"'$") > 0 { printf " %s", $1 }'
71 [ "$1" = "$i" ] && return 0
82 is_in
$i "$2" || unknown
="$unknown $i"
91 is_in
$j "$1" && RESULT
="$RESULT $j"
104 if [ -n "$ARG_DAEMONS" ]
106 echo "${ME}: watchrestart mode is only for use by watchquagga"
115 # select daemons to start
119 start|restart|watchrestart
)
121 for d
in ${DAEMONS_STARTSEQ}
123 [ -x "${BINDIR}/${d}" -a -f "${CONFDIR}/${d}.conf" ] \
124 && START_DAEMONS
="${START_DAEMONS}${d} "
126 WATCHQUAGGA_DAEMONS
=${START_DAEMONS}
127 if is_in watchquagga
"${DAEMONS_STARTSEQ}"
129 START_DAEMONS
="${START_DAEMONS} watchquagga"
131 if [ -n "${ARG_DAEMONS}" ]
133 if select_subset
"${ARG_DAEMONS}" "${DAEMONS}"
135 if select_subset
"${ARG_DAEMONS}" "${START_DAEMONS}"
137 START_DAEMONS
=${RESULT}
139 die
"these daemons are not startable:${RESULT}."
142 die
"unknown daemons:${RESULT}; choose from: ${DAEMONS}."
148 # select daemons to stop
152 stop|restart|watchrestart
)
153 STOP_DAEMONS
=${DAEMONS_STOPSEQ}
154 if [ -n "${ARG_DAEMONS}" ]
156 if select_subset
"${ARG_DAEMONS}" "${STOP_DAEMONS}"
158 STOP_DAEMONS
=${RESULT}
160 die
"unknown daemons:${RESULT}; choose from: ${DAEMONS}."
164 for d
in ${STOP_DAEMONS}
166 pidfile
=${STATEDIR}/${d}.pid
167 if [ -f "${pidfile}" -o -n "$(pidof ${d})" ]
169 stop_daemons
="${stop_daemons}${d} "
170 elif [ -n "${ARG_DAEMONS}" ]
172 echo "${ME}: found no ${d} process running."
175 STOP_DAEMONS
=${stop_daemons}
181 for d
in $STOP_DAEMONS
183 echo -n "${ME}: Stopping ${d} ... "
184 pidfile
=${STATEDIR}/${d}.pid
185 if [ -f "${pidfile}" ]
187 file_pid
=$
(cat ${pidfile})
188 if [ -z "${file_pid}" ]
190 echo -n "no pid file entry found ... "
194 echo -n "no pid file found ... "
196 proc_pid
=$
(pidof
${d})
197 if [ -z "${proc_pid}" ]
199 echo -n "found no ${d} process running ... "
205 count
=$
((${count}+1))
208 echo -n "killed ${p} ... "
210 echo -n "failed to kill ${p} ... "
212 [ "${p}" = "${file_pid}" ] \
213 || notinpidfile
="${notinpidfile} ${p}"
216 ||
echo -n "WARNING: ${count} ${d} processes were found running ... "
217 for n
in ${notinpidfile}
219 echo -n "WARNING: process ${n} was not in pid file ... "
223 survivors
=$
(pidof
${d})
224 while [ -n "${survivors}" ]
227 count
=$
((${count}+1))
228 survivors
=$
(pidof
${d})
229 [ -z "${survivors}" -o ${count} -gt 5 ] && break
230 for p
in ${survivors}
237 survivors
=$
(pidof
${d})
238 [ -n "${survivors}" ] && \
239 if kill -KILL ${survivors}
241 echo -n "KILLed ${survivors} ... "
243 echo -n "failed to KILL ${survivors} ... "
246 survivors
=$
(pidof
${d})
247 if [ -z "${survivors}" ]
250 if [ -f "${pidfile}" ]
253 ||
echo -n " Failed to remove pidfile."
256 echo -n "failed to stop ${survivors} - giving up."
257 if [ "${survivors}" != "${file_pid}" ]
259 if echo "${survivors}" > ${pidfile}
261 chown quagga
:quagga
${pidfile}
262 echo -n " Wrote ${survivors} to pidfile."
264 echo -n " Failed to write ${survivors} to pidfile."
273 if [ -n "$START_DAEMONS" ]
276 || quit
"${ME}: no config directory ${CONFDIR} - exiting."
277 chown
-R quagga
:quagga
${CONFDIR}
278 [ -d ${STATEDIR} ] || mkdir
-p ${STATEDIR} \
279 || die
"${ME}: could not create state directory ${STATEDIR} - exiting."
280 chown
-R quagga
:quagga
${STATEDIR}
282 for d
in $START_DAEMONS
284 echo -n "${ME}: Starting ${d} ... "
285 proc_pid
=$
(pidof
${d})
286 pidfile
=${STATEDIR}/${d}.pid
288 if [ -f "${pidfile}" ]
290 file_pid
=$
(cat ${pidfile})
291 if [ -n "${file_pid}" ]
293 echo -n "found old pid file entry ${file_pid} ... "
296 if [ -n "${proc_pid}" ]
298 echo -n "found ${d} running (${proc_pid}) - skipping ${d}."
299 if [ "${proc_pid}" != "${file_pid}" ]
301 if echo "${proc_pid}" > ${pidfile}
303 chown quagga
:quagga
${pidfile}
304 echo -n " Wrote ${proc_pid} to pidfile."
306 echo -n " Failed to write ${proc_pid} to pidfile."
309 elif rm -f "${pidfile}"
311 if [ "${d}" = "watchquagga" ]
314 ${WATCHQUAGGA_FLAGS} \
315 "${WATCHQUAGGA_CMD}" \
316 ${WATCHQUAGGA_DAEMONS})
319 $
("${BINDIR}/${d}" ${DAEMON_FLAGS})
329 echo -n " failed to remove pidfile.
"