diff options
author | Christian Poessinger <christian@poessinger.com> | 2021-08-13 21:15:58 +0200 |
---|---|---|
committer | Christian Poessinger <christian@poessinger.com> | 2021-08-13 21:33:09 +0200 |
commit | 2b8854761c8ed419b2a2f1e02810c3f68f1d72b6 (patch) | |
tree | 1f9770c136cc5a107fbf1f35f6b671b5481128eb /python/vyos/util.py | |
parent | da94e0a736874d9a6420ec1aa754efcec684b390 (diff) | |
download | vyos-1x-2b8854761c8ed419b2a2f1e02810c3f68f1d72b6.tar.gz vyos-1x-2b8854761c8ed419b2a2f1e02810c3f68f1d72b6.zip |
vyos.util: "harden" is_systemd_service_running() function
Inspired by the comments of https://unix.stackexchange.com/a/435317 use a more
robust approach. A service can be "active" but not "running" (e.g. restarting
with a configuration error). We can now test if a systemd unit is "activated"
and if it is "running" at all.
>>> from vyos.util import is_systemd_service_active
>>> from vyos.util import is_systemd_service_running
>>> is_systemd_service_active('ssh')
True
>>> is_systemd_service_running('sshd')
False
>>> is_systemd_service_running('ssh')
True
Diffstat (limited to 'python/vyos/util.py')
-rw-r--r-- | python/vyos/util.py | 14 |
1 files changed, 11 insertions, 3 deletions
diff --git a/python/vyos/util.py b/python/vyos/util.py index 59f9f1c44..60171746a 100644 --- a/python/vyos/util.py +++ b/python/vyos/util.py @@ -805,8 +805,16 @@ def make_incremental_progressbar(increment: float): while True: yield +def is_systemd_service_active(service): + """ Test is a specified systemd service is activated. + Returns True if service is active, false otherwise. + Copied from: https://unix.stackexchange.com/a/435317 """ + tmp = cmd(f'systemctl show --value -p ActiveState {service}') + return bool((tmp == 'active')) + def is_systemd_service_running(service): """ Test is a specified systemd service is actually running. - Returns True if service is running, false otherwise. """ - tmp = run(f'systemctl is-active --quiet {service}') - return bool((tmp == 0)) + Returns True if service is running, false otherwise. + Copied from: https://unix.stackexchange.com/a/435317 """ + tmp = cmd(f'systemctl show --value -p SubState {service}') + return bool((tmp == 'running')) |