From 0be480452f36b362d0fcbf424e2e62787b423b8d Mon Sep 17 00:00:00 2001 From: ZachIR Date: Wed, 31 Aug 2022 02:30:39 -0500 Subject: add servicectl (to eventually be init system agnostic service frontend) --- servicectl | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100755 servicectl (limited to 'servicectl') diff --git a/servicectl b/servicectl new file mode 100755 index 0000000..d8201ec --- /dev/null +++ b/servicectl @@ -0,0 +1,98 @@ +#!/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 [ "$INIT" = "openrc-init" ]; then + OPENRC=y + unset RUNIT S6 DINIT +fi + +## CHECK PRIVS + +if [ "$(whoami)" != "root" ]; then + ROOTCMD="sudo" +fi + +## SERVICE FUNCTIONS + +start_service () { + if [ -n "$OPENRC" ]; then + $ROOTCMD rc-service $1 start + fi +} + +stop_service () { + if [ -n "$OPENRC" ]; then + $ROOTCMD rc-service $1 stop + fi +} + +enable_service () { + if [ -n "$OPENRC" ]; then + $ROOTCMD rc-update add $1 default + fi +} + +disable_service () { + if [ -n "$OPENRC" ]; then + $ROOTCMD rc-update del $1 default + 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 + ;; +esac -- cgit v1.2.3