#!/usr/bin/env bash ## CHECK DEPS if ! type readlink >/dev/null 2>&1; then printf "readlink command not found.\n" exit 1 fi ## UTIL FUNCTIONS reverse_array () { unset _b for a in $@; do [ -n "$_b" ] && _b="$a $_b" || _b="$a" done echo $_b } ## FIND INIT INIT="$(readlink $(type init) )" if `type s6-rc >/dev/null 2>&1`; then S6=y else unset S6 fi case "$INIT" in "openrc-init") OPENRC=y ; unset RUNIT DINIT ;; "runit-init") RUNIT=y ; unset OPENRC DINIT ;; "*") print "Unknown init!\n" esac ## CHECK INIT HELPER if [ -n "$RUNIT" ]; then if `type rsm >/dev/null 2>&1`; then RSM=y else unset RSM fi fi ## CHECK PRIVS if [ "$(whoami)" != "root" ]; then ROOTCMD="sudo" fi ## SERVICE FUNCTIONS start_service () { if [ -n "$S6" ]; then $ROOTCMD s6-rc -u change $1 fi if [ -n "$OPENRC" ]; then $ROOTCMD rc-service $1 start elif [ -n "$RUNIT" ]; then if [ -n "$RSM" ]; then $ROOTCMD rsm start $1 else $ROOTCMD sv start $1 fi fi } stop_service () { if [ -n "$S6" ]; then $ROOTCMD s6-rc -d change $1 fi if [ -n "$OPENRC" ]; then $ROOTCMD rc-service $1 stop elif [ -n "$RUNIT" ]; then if [ -n "$RSM" ]; then $ROOTCMD rsm stop $1 else $ROOTCMD sv stop $1 fi fi } enable_service () { if [ -n "$S6" ]; then $ROOTCMD s6-service add default $1 fi if [ -n "$OPENRC" ]; then $ROOTCMD rc-update add $1 default elif [ -n "$RUNIT" ]; then if [ -n "$RSM" ]; then $ROOTCMD rsm enable $1 else print "enabling runit services manually not presently supported; use rsm\n" fi fi } disable_service () { if [ -n "$S6" ]; then $ROOTCMD s6-service delete default $1 fi if [ -n "$OPENRC" ]; then $ROOTCMD rc-update del $1 default elif [ -n "$RUNIT" ]; then if [ -n "$RSM" ]; then $ROOTCMD rsm disable $1 else print "disabling runit services manually not presently supported; use rsm\n" fi fi } status_service () { if [ -n "$S6" ]; then $ROOTCMD s6-rc -a list fi if [ -n "$OPENRC" ]; then $ROOTCMD rc-service status elif [ -n "$RUNIT" ]; then if [ -n "$RSM" ]; then $ROOTCMD rsm status else print "getting status of runit services manually not presently supported; use rsm\n" fi fi } ## MAIN LOOP case "$1" in "start") for i in "${@:2}"; do printf "start $i\n" start_service $i done ;; "stop") for i in "${@:2}"; do printf "stop $i\n" stop_service $i done ;; "enable") for i in "${@:2}"; do printf "enable $i\n" enable_service $i done ;; "disable") for i in "${@:2}"; do printf "disable $i\n" disable_service $i done ;; "restart") for i in `reverse_array ${@:2}`; do printf "stop $i\n" stop_service $i done for i in ${@:2}; do printf "start $i\n" start_service $i done ;; "status") status_service esac