fix: improve service_control.sh

This commit is contained in:
th33xitus 2022-02-23 19:14:39 +01:00
parent eafc13026e
commit fe74614762
1 changed files with 64 additions and 66 deletions

View File

@ -11,35 +11,34 @@ MY_NAME="${0##*/}"
PARSED_ARGUMENTS="$(getopt -n "$MY_NAME" -o t:p:a --long type:,property:,all,value,no-legend,plain -- "$@")" || exit $?
eval set -- "$PARSED_ARGUMENTS"
while :
do
while true; do
case "$1" in
-t | --type)
shift 2
;;
-p | --property)
IFS="," read -a PROPERTIES <<< $2
shift 2
;;
-a | --all)
shift
;;
--value)
shift
;;
--no-legend)
shift
;;
--plain)
shift
;;
--)
shift;
break
;;
*)
die "unexpected option: $1 - this should not happen."
;;
-t | --type)
shift 2
;;
-p | --property)
IFS="," read -a PROPERTIES <<<$2
shift 2
;;
-a | --all)
shift
;;
--value)
shift
;;
--no-legend)
shift
;;
--plain)
shift
;;
--)
shift
break
;;
*)
die "unexpected option: $1 - this should not happen."
;;
esac
done
@ -49,48 +48,47 @@ shift
UNITS=($@)
case "$COMMAND" in
show)
for UNIT in "${UNITS[@]}"
do
for PROPERTY in "${PROPERTIES[@]}"
do
case "$PROPERTY" in
LoadState)
echo "loaded"
;;
ActiveState)
echo "active"
;;
SubState)
sudo /usr/bin/supervisorctl status "$UNIT" | awk '{print tolower($2)}'
;;
*)
echo "unknown"
;;
esac
done
echo ""
show)
for UNIT in "${UNITS[@]}"; do
UNIT_STATE=$(sudo /usr/bin/supervisorctl status "$UNIT" | awk '{print tolower($2)}')
for PROPERTY in "${PROPERTIES[@]}"; do
case "$PROPERTY" in
LoadState)
echo "loaded"
;;
ActiveState)
[ "$UNIT_STATE" = "running" ] && echo "active" || echo "inactive"
;;
SubState)
[ "$UNIT_STATE" = "running" ] && echo "running" || echo "dead"
;;
*)
echo "unknown"
;;
esac
done
;;
echo ""
done
;;
list-units)
sudo /usr/bin/supervisorctl status | awk '{print $1".service\tloaded\tactive\t"tolower($2)"\t"$1" service"}'
;;
list-units)
sudo /usr/bin/supervisorctl status | awk '{print $1".service\tloaded\t"(tolower($2)=="running"?"active\trunning":"inactive\tdead")"\t"$1" service"}'
;;
start | stop | restart)
for UNIT in "${UNITS[@]}"
do
if [ "$UNIT" = "klipper" ]; then
sudo /usr/bin/supervisorctl "$COMMAND" simulavr klipper
else
sudo /usr/bin/supervisorctl "$COMMAND" "$UNIT"
fi
done
;;
start | stop | restart)
for UNIT in "${UNITS[@]}"; do
if [ "$UNIT" = "klipper" ]; then
sudo /usr/bin/supervisorctl "$COMMAND" simulavr klipper
else
sudo /usr/bin/supervisorctl "$COMMAND" "$UNIT"
fi
done
;;
*)
die "unknown command '$COMMAND'"
;;
*)
die "unknown command '$COMMAND'"
;;
esac
exit 0