From: Christian Herdtweck Date: Thu, 25 Apr 2024 10:40:41 +0000 (+0200) Subject: Add tool to query state of system service X-Git-Tag: v1.7.3~1^2~4 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=6ae1ef20450856035a52adcb72f3a68bdd17a5a6;p=pyi2ncommon Add tool to query state of system service I have written this or very similar code a few times, time to centralize it. --- diff --git a/src/sysmisc.py b/src/sysmisc.py index 79b8f85..3a2b0cd 100644 --- a/src/sysmisc.py +++ b/src/sysmisc.py @@ -73,7 +73,6 @@ The library exports the symbols below and some custom logging functions. The logging functions either use the format capability or play the simple role of providing shorter names. """ - import re import subprocess import hashlib @@ -83,6 +82,7 @@ import time import types import uuid from contextlib import contextmanager +import enum import logging llog = logging.getLogger('pyi2ncommon.sysmisc') @@ -239,6 +239,41 @@ def hash_file(fname, new=hashlib.sha512, bs=4096): return hsh.hexdigest() +class ServiceState(enum.Enum): + """State of a system service, see `get_service_state`.""" + RUNNING = 0 + DEAD_WITH_PIDFILE = 1 + DEAD_WITH_LOCKFILE = 2 + STOPPED = 3 + SERVICE_UNKNOWN = 4 + OTHER = -1 + + +def get_service_state(service_name: str) -> ServiceState: + """ + Get state of a system service. + + Calls "service {service_name} status", since that works on older and newer systemd-based + installs, and tries to interpret return code as defined by the + "Linux Standard Base PDA Specification 3.0RC1", Chapter 8.2 ("Init Script Actions", + https://refspecs.linuxbase.org/LSB_3.0.0/LSB-PDA/LSB-PDA/iniscrptact.html ). + """ + code = subprocess.run(["service", service_name, "status"], + capture_output=True).returncode + if code == 0: + return ServiceState.RUNNING + elif code == 1: + return ServiceState.DEAD_WITH_PIDFILE + elif code == 2: + return ServiceState.DEAD_WITH_LOCKFILE + elif code == 3: + return ServiceState.STOPPED + elif code == 4: + return ServiceState.SERVICE_UNKNOWN + else: + return ServiceState.OTHER + + cheat_tpl = """\ #!/bin/sh set -u