#!/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; do [ -n "$_b" ] && _b="$a $_b" || _b="$a" done echo "$_b" } ## FIND INIT INIT="$(basename "$(readlink "$(type init)")")" if type s6-rc >/dev/null 2>&1; then S6=y else unset S6 fi if type dinitctl >/dev/null 2>&1; then DINIT=y else unset DINIT fi case "$INIT" in "openrc-init") OPENRC=y ; unset DINIT RUNIT SYSTEMD ;; "runit-init") RUNIT=y ; unset DINIT OPENRC SYSTEMD ;; "systemd") SYSTEMD=y ; unset DINIT OPENRC RUNIT ;; "*") 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 "$DINIT" ]; then $ROOTCMD dinitctl start "$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 elif [ -n "$SYSTEMD" ]; then $ROOTCMD systemctl start "$1" fi } stop_service () { if [ -n "$S6" ]; then $ROOTCMD s6-rc -d change "$1" fi if [ -n "$DINIT" ]; then $ROOTCMD dinitctl stop "$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 elif [ -n "$SYSTEMD" ]; then $ROOTCMD systemctl stop "$1" fi } enable_service () { if [ -n "$S6" ]; then $ROOTCMD s6-service add default "$1" fi if [ -n "$DINIT" ]; then $ROOTCMD dinitctl enable "$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 elif [ -n "$SYSTEMD" ]; then $ROOTCMD systemctl enable "$1" fi } disable_service () { if [ -n "$S6" ]; then $ROOTCMD s6-service delete default "$1" fi if [ -n "$DINIT" ]; then $ROOTCMD dinitctl disable "$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 elif [ -n "$SYSTEMD" ]; then $ROOTCMD systemctl disable "$1" fi } status_service () { if [ -n "$S6" ]; then $ROOTCMD s6-rc -a list fi if [ -n "$DINIT" ]; then $ROOTCMD dinitcheck 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 elif [ -n "$SYSTEMD" ]; then $ROOTCMD systemctl status fi } ## MAIN LOOP case "$1" in "start") for i in "${@:2}"; do printf "start %s\n" "$i" start_service "$i" done ;; "stop") for i in "${@:2}"; do printf "stop %s\n" "$i" stop_service "$i" done ;; "enable") for i in "${@:2}"; do printf "enable %s\n" "$i" enable_service "$i" done ;; "disable") for i in "${@:2}"; do printf "disable %s\n" "$i" disable_service "$i" done ;; "restart") read -ra services < <(echo "${@:2}") read -ra reversed < <(reverse_array "${services[@]}") for service in "${reversed[@]}"; do printf "stop %s\n" "$service" stop_service "$service" done for service in "${services[@]}"; do printf "start %s\n" "$service" start_service "$service" done ;; "status") status_service esac