Added new rc.<service> scripts - various.
This commit is contained in:
parent
e780b3e191
commit
7fcce6c9d3
5 changed files with 487 additions and 135 deletions
103
rc.d/rc.rsyncd
103
rc.d/rc.rsyncd
|
|
@ -1,63 +1,78 @@
|
|||
#!/bin/bash
|
||||
# Version: 0.2.9
|
||||
# Copyright (c) 2005-2017:
|
||||
# Version: 0.3.0
|
||||
# Copyright (c) 2005-2022:
|
||||
# Darren 'Tadgy' Austin <darren (at) afterdark.org.uk>
|
||||
# Licensed under the terms of the GNU General Public License version 3.
|
||||
|
||||
EXEC="/usr/bin/rsync"
|
||||
ARGS=(--daemon --config=/etc/rsyncd/rsyncd.conf)
|
||||
PIDFILE="/var/run/rsyncd.pid"
|
||||
ARGS=('--daemon' '--config=/etc/rsyncd/rsyncd.conf')
|
||||
PIDFILE="/run/rsyncd.pid"
|
||||
|
||||
# Allow configuration in /etc/default to override.
|
||||
# Additional available variables:
|
||||
# ENVIRONMENT=() # Extra environment passed to $EXEC. Must be an array.
|
||||
# EXTRA_ARGS=() # Extra arguments passed to $EXEC. Must be an array.
|
||||
# SLAY_DELAY="" # Delay between the SIGTERM and SIGKILL on a 'stop'. Default: 2s.
|
||||
# RESTART_DELAY="" # Delay between stopping and starting on a 'restart'. Default: 2s.
|
||||
[[ -e "/etc/default/${0##*rc.}" ]] && source "/etc/default/${0##*rc.}"
|
||||
|
||||
error() {
|
||||
printf "%s: %s: %s\\n" "${BASH_SOURCE##*/}" "${EXEC##*/}" "$*" >&2
|
||||
}
|
||||
|
||||
checkconfigured() {
|
||||
# This function can be used to perform any pre-start tests; hopfully to insure the daemon
|
||||
# can start correctly, before actually trying to start it. A return value of 0 means the
|
||||
# tests were passed and the daemon should be started. Any other value prevents the
|
||||
# daemon from being started and an error message will be emitted.
|
||||
# daemon from being started, and an error message will be emitted.
|
||||
return 0
|
||||
}
|
||||
|
||||
checkstatus() {
|
||||
local RUNPIDS="$(pgrep -f "$EXEC")"
|
||||
if [ ! -z "$RUNPIDS" ]; then
|
||||
echo -n "${BASH_SOURCE##*/}: ${EXEC##*/}: running"
|
||||
if [ ! -z "$PIDFILE" ]; then
|
||||
if [ ! -e "$PIDFILE" ]; then
|
||||
echo -n ", but .pid file does not exist"
|
||||
elif ! echo "$RUNPIDS" | grep "\<$(cat "$PIDFILE")\>" >/dev/null 2>&1; then
|
||||
echo -n ", but .pid file is stale"
|
||||
local RUNPIDS="$({ pgrep -f "$EXEC"; pgrep -F "$PIDFILE" 2>/dev/null; } | sort -u )"
|
||||
if [[ ! -z "$RUNPIDS" ]]; then
|
||||
printf "%s: %s: %s" "${BASH_SOURCE##*/}" "${EXEC##*/}" "running"
|
||||
if [[ ! -z "$PIDFILE" ]]; then
|
||||
if [[ ! -e "$PIDFILE" ]]; then
|
||||
printf "%s" ", but .pid file does not exist"
|
||||
elif ! grep "\<$(< "$PIDFILE")\>" <<<"$RUNPIDS" >/dev/null 2>&1; then
|
||||
printf "%s" ", but .pid file is stale"
|
||||
fi
|
||||
fi
|
||||
echo
|
||||
printf "\\n"
|
||||
else
|
||||
echo "${BASH_SOURCE##*/}: ${EXEC##*/}: stopped"
|
||||
printf "%s: %s: %s\\n" "${BASH_SOURCE##*/}" "${EXEC##*/}" "stopped"
|
||||
return 1
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
startdaemon() {
|
||||
if ! checkconfigured; then
|
||||
echo "${BASH_SOURCE##*/}: ${EXEC##*/}: not started - pre-start checks failed" >&2
|
||||
if [[ ! -e "$EXEC" ]]; then
|
||||
error "not found"
|
||||
return 1
|
||||
elif [ ! -e "$EXEC" ]; then
|
||||
echo "${BASH_SOURCE##*/}: ${EXEC##*/}: not found" >&2
|
||||
elif [[ ! -x "$EXEC" ]]; then
|
||||
error "not executable"
|
||||
return 1
|
||||
elif [ ! -x "$EXEC" ]; then
|
||||
echo "${BASH_SOURCE##*/}: ${EXEC##*/}: not executable" >&2
|
||||
elif ! checkconfigured; then
|
||||
error "not started - pre-start checks failed"
|
||||
return 1
|
||||
fi
|
||||
"$EXEC" "${ARGS[@]}"
|
||||
return $?
|
||||
${ENVIRONMENT:+declare ${ENVIRONMENT[*]};} "$EXEC" ${ARGS[*]} ${EXTRA_ARGS[*]}
|
||||
if (( $? != 0 )); then
|
||||
error "error starting daemon"
|
||||
return 1
|
||||
else
|
||||
return 0
|
||||
fi
|
||||
}
|
||||
|
||||
stopdaemon() {
|
||||
if ! kill -TERM "$(cat "$PIDFILE" 2>/dev/null)" >/dev/null 2>&1; then
|
||||
kill -TERM "$(pgrep -f "$EXEC")" >/dev/null 2>&1
|
||||
fi
|
||||
sleep 2
|
||||
[[ -e "$PIDFILE" ]] && kill -TERM "$(< "$PIDFILE")" >/dev/null 2>&1 || kill -TERM "$(pgrep -f "$EXEC" | tr $'\n' " ")" >/dev/null 2>&1
|
||||
sleep "${SLAY_DELAY:-2}"
|
||||
if checkstatus >/dev/null; then
|
||||
echo "${BASH_SOURCE##*/}: ${EXEC##*/}: failed to stop gracefully - slaying" >&2
|
||||
kill -KILL "$(pgrep -f "$EXEC")" >/dev/null 2>&1
|
||||
error "failed to stop gracefully - slaying"
|
||||
kill -KILL "$({ cat "$PIDFILE"; pgrep -f "$EXEC"; } 2>/dev/null | sort -u | tr $'\n' " ")" >/dev/null 2>&1
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
|
@ -65,41 +80,41 @@ stopdaemon() {
|
|||
case "$1" in
|
||||
'start')
|
||||
if checkstatus >/dev/null; then
|
||||
echo "${BASH_SOURCE##*/}: ${EXEC##*/}: already running" >&2
|
||||
echo " Try: $BASH_SOURCE status" >&2
|
||||
ERR=1
|
||||
error "already running"
|
||||
printf " %s\\n" "Try: $BASH_SOURCE status" >&2
|
||||
RET=1
|
||||
else
|
||||
startdaemon
|
||||
ERR=$?
|
||||
RET=$?
|
||||
fi
|
||||
;;
|
||||
'stop')
|
||||
if ! checkstatus >/dev/null; then
|
||||
echo "${BASH_SOURCE##*/}: ${EXEC##*/}: not running" >&2
|
||||
echo " Try: $BASH_SOURCE status" >&2
|
||||
ERR=1
|
||||
error "not running"
|
||||
printf " %s\\n" "Try: $BASH_SOURCE status" >&2
|
||||
RET=1
|
||||
else
|
||||
stopdaemon
|
||||
ERR=$?
|
||||
RET=$?
|
||||
fi
|
||||
;;
|
||||
'restart')
|
||||
if checkstatus >/dev/null; then
|
||||
stopdaemon && sleep 2 && startdaemon
|
||||
ERR=$?
|
||||
stopdaemon && sleep "${RESTART_DELAY:-2}" && startdaemon
|
||||
RET=$?
|
||||
else
|
||||
startdaemon
|
||||
ERR=$?
|
||||
RET=$?
|
||||
fi
|
||||
;;
|
||||
'status')
|
||||
checkstatus
|
||||
ERR=$?
|
||||
RET=$?
|
||||
;;
|
||||
*)
|
||||
echo "Usage: $BASH_SOURCE <start|stop|restart|status>" >&2
|
||||
ERR=1
|
||||
printf "%s\\n" "Usage: $BASH_SOURCE <start|stop|restart|status>" >&2
|
||||
RET=1
|
||||
;;
|
||||
esac
|
||||
|
||||
return $ERR 2>/dev/null || exit $ERR
|
||||
return $RET 2>/dev/null || exit $RET
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue