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
32 DAEMONS
="zebra ripd ripngd ospfd ospf6d bgpd"
34 WATCHQUAGGA_FLAGS
="-d -z -T 60 -R"
35 WATCHQUAGGA_CMD
="$0 watchrestart"
36 if [ ${COMMAND} != "watchrestart" -a -x "${BINDIR}/watchquagga" ]
38 DAEMONS
="${DAEMONS} watchquagga"
40 DAEMONS_STARTSEQ
=${DAEMONS}
53 DAEMONS_STOPSEQ
=$
(reverse
${DAEMONS_STARTSEQ})
56 # ps ax | awk 'match($5, "(^|/)'"$1"'$") > 0 { printf " %s", $1 }'
73 [ "$1" = "$i" ] && return 0
84 is_in
$i "$2" || unknown
="$unknown $i"
93 is_in
$j "$1" && RESULT
="$RESULT $j"
106 if [ -n "$ARG_DAEMONS" ]
108 echo "${ME}: watchrestart mode is only for use by watchquagga"
117 # select daemons to start
121 start|restart|watchrestart
)
123 for d
in ${DAEMONS_STARTSEQ}
125 [ -x "${BINDIR}/${d}" -a -f "${CONFDIR}/${d}.conf" ] \
126 && START_DAEMONS
="${START_DAEMONS}${d} "
128 WATCHQUAGGA_DAEMONS
=${START_DAEMONS}
129 if is_in watchquagga
"${DAEMONS_STARTSEQ}"
131 START_DAEMONS
="${START_DAEMONS} watchquagga"
133 if [ -n "${ARG_DAEMONS}" ]
135 if select_subset
"${ARG_DAEMONS}" "${DAEMONS}"
137 if select_subset
"${ARG_DAEMONS}" "${START_DAEMONS}"
139 START_DAEMONS
=${RESULT}
141 die
"these daemons are not startable:${RESULT}."
144 die
"unknown daemons:${RESULT}; choose from: ${DAEMONS}."
150 # select daemons to stop
154 stop|restart|watchrestart
)
155 STOP_DAEMONS
=${DAEMONS_STOPSEQ}
156 if [ -n "${ARG_DAEMONS}" ]
158 if select_subset
"${ARG_DAEMONS}" "${STOP_DAEMONS}"
160 STOP_DAEMONS
=${RESULT}
162 die
"unknown daemons:${RESULT}; choose from: ${DAEMONS}."
166 for d
in ${STOP_DAEMONS}
168 pidfile
=${STATEDIR}/${d}.pid
169 if [ -f "${pidfile}" -o -n "$(pidof ${d})" ]
171 stop_daemons
="${stop_daemons}${d} "
172 elif [ -n "${ARG_DAEMONS}" ]
174 echo "${ME}: found no ${d} process running."
177 STOP_DAEMONS
=${stop_daemons}
183 for d
in $STOP_DAEMONS
185 echo -n "${ME}: Stopping ${d} ... "
186 pidfile
=${STATEDIR}/${d}.pid
187 if [ -f "${pidfile}" ]
189 file_pid
=$
(cat ${pidfile})
190 if [ -z "${file_pid}" ]
192 echo -n "no pid file entry found ... "
196 echo -n "no pid file found ... "
198 proc_pid
=$
(pidof
${d})
199 if [ -z "${proc_pid}" ]
201 echo -n "found no ${d} process running ... "
207 count
=$
((${count}+1))
210 echo -n "killed ${p} ... "
212 echo -n "failed to kill ${p} ... "
214 [ "${p}" = "${file_pid}" ] \
215 || notinpidfile
="${notinpidfile} ${p}"
218 ||
echo -n "WARNING: ${count} ${d} processes were found running ... "
219 for n
in ${notinpidfile}
221 echo -n "WARNING: process ${n} was not in pid file ... "
225 survivors
=$
(pidof
${d})
226 while [ -n "${survivors}" ]
229 count
=$
((${count}+1))
230 survivors
=$
(pidof
${d})
231 [ -z "${survivors}" -o ${count} -gt 5 ] && break
232 for p
in ${survivors}
239 survivors
=$
(pidof
${d})
240 [ -n "${survivors}" ] && \
241 if kill -KILL ${survivors}
243 echo -n "KILLed ${survivors} ... "
245 echo -n "failed to KILL ${survivors} ... "
248 survivors
=$
(pidof
${d})
249 if [ -z "${survivors}" ]
252 if [ -f "${pidfile}" ]
255 ||
echo -n " Failed to remove pidfile."
258 echo -n "failed to stop ${survivors} - giving up."
259 if [ "${survivors}" != "${file_pid}" ]
261 if echo "${survivors}" > ${pidfile}
263 chown
${RUNUSER}:${RUNGROUP} ${pidfile}
264 echo -n " Wrote ${survivors} to pidfile."
266 echo -n " Failed to write ${survivors} to pidfile."
275 if [ -n "$START_DAEMONS" ]
278 || quit
"${ME}: no config directory ${CONFDIR} - exiting."
279 chown
-R ${RUNUSER}:${RUNGROUP} ${CONFDIR}
280 [ -d ${STATEDIR} ] || mkdir
-p ${STATEDIR} \
281 || die
"${ME}: could not create state directory ${STATEDIR} - exiting."
282 chown
-R ${RUNUSER}:${RUNGROUP} ${STATEDIR}
284 for d
in $START_DAEMONS
286 echo -n "${ME}: Starting ${d} ... "
287 proc_pid
=$
(pidof
${d})
288 pidfile
=${STATEDIR}/${d}.pid
290 if [ -f "${pidfile}" ]
292 file_pid
=$
(cat ${pidfile})
293 if [ -n "${file_pid}" ]
295 echo -n "found old pid file entry ${file_pid} ... "
298 if [ -n "${proc_pid}" ]
300 echo -n "found ${d} running (${proc_pid}) - skipping ${d}."
301 if [ "${proc_pid}" != "${file_pid}" ]
303 if echo "${proc_pid}" > ${pidfile}
305 chown
${RUNUSER}:${RUNGROUP} ${pidfile}
306 echo -n " Wrote ${proc_pid} to pidfile."
308 echo -n " Failed to write ${proc_pid} to pidfile."
311 elif rm -f "${pidfile}"
313 if [ "${d}" = "watchquagga" ]
316 ${WATCHQUAGGA_FLAGS} \
317 "${WATCHQUAGGA_CMD}" \
318 ${WATCHQUAGGA_DAEMONS}
321 "${BINDIR}/${d}" ${DAEMON_FLAGS}
331 echo -n " failed to remove pidfile.
"