blob: 12d8220ef60ceb5731ac4536f7b06a736db3eec3 [file] [log] [blame]
#!/bin/sh
#
# A handy command to start/stop/restart/powerdown daemons using scripts in
# /etc/init.d or /service.
#
mode=$(basename "$0")
log() {
echo "$@" >&2
}
usage() {
log "Usage: start|stop|restart|powerdown <programs...>"
exit 99
}
endswith() {
[ "$1" != "${1%$2}" ]
}
suggestions() {
local prog="$1" d
for d in /etc/init.d/S??*; do
if { echo "$d"; cat "$d"; } | grep -q "$prog"; then
echo "${d#/etc/init.d/S??}(${d#/etc/init.d/})"
fi
done
for d in /service/*; do
[ "$d" = "/service/s6rc-fdholder" ] && continue
endswith "$d" "-log" && continue
if { echo "$d"; cat $d/run* 2>/dev/null; } | grep -q "$prog"; then
echo "${d#/service/}($d)"
fi
done
}
case "$mode" in
start|stop|restart|powerdown) ;;
*) usage ;;
esac
rv=0
any=
for prog in "$@"; do
any=1
prog=${prog##*/}
if [ -z "$prog" ]; then
log "Error: invalid program name '$prog'"
rv=1
continue
fi
# New-style services (s6)
if s6-rc-db type "$prog" >/dev/null 2>/dev/null; then
# Don't need to worry about lockfile-create/lockfile-remove here;
# s6 already ensures services don't start more than once.
case "$mode" in
start)
log "Starting /service/$prog..."
s6-rc -v2 -u change "$prog"
;;
stop)
log "Stopping /service/$prog..."
s6-rc -v2 -d -O change "$prog"
;;
restart)
log "Restarting /service/$prog..."
s6-rc -v2 -d -O change "$prog" &&
s6-rc -v2 -u -O change "$prog"
;;
powerdown)
log "Error: powerdown not supported for '/service/$prog'"
false
;;
esac
rv=$?
continue # found a match
fi
# Old-style services (/etc/init.d)
prog=${prog#S??}
lockfile-create --use-pid "/var/run/startstop.$prog"
found=
for script in /etc/init.d/S??$prog /etc/init.d/S??$prog.sh; do
if [ -x "$script" ]; then
found=1
gsetsid "$script" "$mode" || rv=1
break
elif [ -e "$script" ] && endswith "$script" .sh; then
found=1
gsetsid /bin/sh "$script" "$mode" || rv=1
break
fi
done
if [ -z "$found" ] && [ -z "$QUIET" ]; then
log "Error: no /etc/init.d or /service script matches '$prog'" >&2
log "Error: suggestion:" $(suggestions "$prog")
rv=1
fi
lockfile-remove "/var/run/startstop.$prog"
done
if [ -z "$any" ]; then
usage
fi
exit $rv