Improve service stopping logic in stop_all_services

Refactored stop_all_services to handle empty service lists and avoid pipeline failures. Now checks for found services before attempting to stop and disable them, improving robustness and error handling.
This commit is contained in:
CanbiZ
2025-11-10 13:58:04 +01:00
parent de07b62cff
commit ef9a008d2a

View File

@@ -72,15 +72,23 @@ stop_all_services() {
local service_patterns=("$@") local service_patterns=("$@")
for pattern in "${service_patterns[@]}"; do for pattern in "${service_patterns[@]}"; do
# Find all matching services # Find all matching services (use || true to avoid pipeline failures)
systemctl list-units --type=service --all 2>/dev/null | local services
grep -oE "${pattern}[^ ]*\.service" | services=$(systemctl list-units --type=service --all 2>/dev/null |
sort -u | grep -oE "${pattern}[^ ]*\.service" 2>/dev/null |
while read -r service; do sort -u 2>/dev/null || true)
# Only process if we found any services
if [[ -n "$services" ]]; then
while IFS= read -r service; do
[[ -z "$service" ]] && continue
$STD systemctl stop "$service" 2>/dev/null || true $STD systemctl stop "$service" 2>/dev/null || true
$STD systemctl disable "$service" 2>/dev/null || true $STD systemctl disable "$service" 2>/dev/null || true
done <<<"$services"
fi
done done
done
return 0
} }
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------