summaryrefslogtreecommitdiff
path: root/servicectl
diff options
context:
space:
mode:
authorzachir <zachir@librem.one>2025-08-30 01:09:18 -0500
committerzachir <zachir@librem.one>2025-08-30 01:09:18 -0500
commitf28d694879239d5d9c319185a278236d317a49b6 (patch)
tree3f684e05c6372c8f0fc0b9f126a184f3aaad68d4 /servicectl
parent7d3cbe8f708772e40608260590b717dea6735908 (diff)
Make sh scrips POSIX compliant
Using shellcheck, I went through all of them to make them standards compliant. I also tested as many as I could.
Diffstat (limited to 'servicectl')
-rwxr-xr-xservicectl97
1 files changed, 60 insertions, 37 deletions
diff --git a/servicectl b/servicectl
index 698d3e4..5e438a7 100755
--- a/servicectl
+++ b/servicectl
@@ -11,21 +11,27 @@ fi
reverse_array () {
unset _b
- for a in $@; do
+ for a; do
[ -n "$_b" ] && _b="$a $_b" || _b="$a"
done
- echo $_b
+ echo "$_b"
}
## FIND INIT
-INIT="$(basename $(readlink $(type init)))"
-if `type s6-rc >/dev/null 2>&1`; then
+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 ;;
@@ -36,7 +42,7 @@ esac
## CHECK INIT HELPER
if [ -n "$RUNIT" ]; then
- if `type rsm >/dev/null 2>&1`; then
+ if type rsm >/dev/null 2>&1; then
RSM=y
else
unset RSM
@@ -53,69 +59,81 @@ fi
start_service () {
if [ -n "$S6" ]; then
- $ROOTCMD s6-rc -u change $1
+ $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
+ $ROOTCMD rc-service "$1" start
elif [ -n "$RUNIT" ]; then
if [ -n "$RSM" ]; then
- $ROOTCMD rsm start $1
+ $ROOTCMD rsm start "$1"
else
- $ROOTCMD sv start $1
+ $ROOTCMD sv start "$1"
fi
elif [ -n "$SYSTEMD" ]; then
- $ROOTCMD systemctl start $1
+ $ROOTCMD systemctl start "$1"
fi
}
stop_service () {
if [ -n "$S6" ]; then
- $ROOTCMD s6-rc -d change $1
+ $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
+ $ROOTCMD rc-service "$1" stop
elif [ -n "$RUNIT" ]; then
if [ -n "$RSM" ]; then
- $ROOTCMD rsm stop $1
+ $ROOTCMD rsm stop "$1"
else
- $ROOTCMD sv stop $1
+ $ROOTCMD sv stop "$1"
fi
elif [ -n "$SYSTEMD" ]; then
- $ROOTCMD systemctl stop $1
+ $ROOTCMD systemctl stop "$1"
fi
}
enable_service () {
if [ -n "$S6" ]; then
- $ROOTCMD s6-service add default $1
+ $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
+ $ROOTCMD rc-update add "$1" default
elif [ -n "$RUNIT" ]; then
if [ -n "$RSM" ]; then
- $ROOTCMD rsm enable $1
+ $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
+ $ROOTCMD systemctl enable "$1"
fi
}
disable_service () {
if [ -n "$S6" ]; then
- $ROOTCMD s6-service delete default $1
+ $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
+ $ROOTCMD rc-update del "$1" default
elif [ -n "$RUNIT" ]; then
if [ -n "$RSM" ]; then
- $ROOTCMD rsm disable $1
+ $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
+ $ROOTCMD systemctl disable "$1"
fi
}
@@ -123,6 +141,9 @@ 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
@@ -141,36 +162,38 @@ status_service () {
case "$1" in
"start")
for i in "${@:2}"; do
- printf "start $i\n"
- start_service $i
+ printf "start %s\n" "$i"
+ start_service "$i"
done
;;
"stop")
for i in "${@:2}"; do
- printf "stop $i\n"
- stop_service $i
+ printf "stop %s\n" "$i"
+ stop_service "$i"
done
;;
"enable")
for i in "${@:2}"; do
- printf "enable $i\n"
- enable_service $i
+ printf "enable %s\n" "$i"
+ enable_service "$i"
done
;;
"disable")
for i in "${@:2}"; do
- printf "disable $i\n"
- disable_service $i
+ printf "disable %s\n" "$i"
+ disable_service "$i"
done
;;
"restart")
- for i in `reverse_array ${@:2}`; do
- printf "stop $i\n"
- stop_service $i
+ 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 i in ${@:2}; do
- printf "start $i\n"
- start_service $i
+ for service in "${services[@]}"; do
+ printf "start %s\n" "$service"
+ start_service "$service"
done
;;
"status")