diff options
author | ZachIR <zachir@librem.one> | 2022-08-31 02:30:39 -0500 |
---|---|---|
committer | ZachIR <zachir@librem.one> | 2022-08-31 02:30:39 -0500 |
commit | 0be480452f36b362d0fcbf424e2e62787b423b8d (patch) | |
tree | 6e8933cce99163150c028c1fad9c869ba6bc3d62 /servicectl | |
parent | af8bace587c631ab64b6f30635a1d41ecbdf85ef (diff) |
add servicectl (to eventually be init system agnostic service frontend)
Diffstat (limited to 'servicectl')
-rwxr-xr-x | servicectl | 98 |
1 files changed, 98 insertions, 0 deletions
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 |