From 15a59d6b2e764164471abd8d270c6eb70cfea0b4 Mon Sep 17 00:00:00 2001 From: damien cavagnini Date: Mon, 28 Jul 2025 16:04:11 +0200 Subject: [PATCH] feat: add 'is_service_active' in lib --- lib/utils.sh | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/lib/utils.sh b/lib/utils.sh index 36c3ebf..d74b7c5 100644 --- a/lib/utils.sh +++ b/lib/utils.sh @@ -326,6 +326,48 @@ is_service_enabled() { fi } +is_service_active() { + local SERVICE=$1 + + # if running in a container, it does not make much sense to test for systemd / service + # the var "IS_CONTAINER" defined in lib/constant may not be enough, in case we are using systemd slices + # currently, did not find a unified way to manage all cases, so we check this only for systemctl usage + is_using_sbin_init + if [ "$FNRET" -eq 1 ]; then + debug "host was not started using '/sbin/init', systemd should not be available" + FNRET=1 + return + fi + if $SUDO_CMD systemctl -t service is-active "$SERVICE" >/dev/null; then + debug "Service $SERVICE is active" + FNRET=0 + else + debug "Service $SERVICE is not active" + FNRET=1 + fi +} + +is_socket_enabled() { + local SOCKET=$1 + + # if running in a container, it does not make much sense to test for systemd / service + # the var "IS_CONTAINER" defined in lib/constant may not be enough, in case we are using systemd slices + # currently, did not find a unified way to manage all cases, so we check this only for systemctl usage + is_using_sbin_init + if [ "$FNRET" -eq 1 ]; then + debug "host was not started using '/sbin/init', systemd should not be available" + FNRET=1 + return + fi + if $SUDO_CMD systemctl -t socket is-enabled "$SOCKET" >/dev/null; then + debug "Socket $SOCKET is enabled" + FNRET=0 + else + debug "Socket $SOCKET is disabled" + FNRET=1 + fi +} + # # Kernel Options checks #