Lightweight Python module wrapper for systemctl
A lightweight Python module to interact with systemd
services via systemctl
, designed for use in Python-based service managers, admin tools, and dashboards.
systemctl
outputIMPORTANT NOTE: All systemctl
operations except for sysemctl status
require root access. This module uses sudo
to deal with this fact. It’s recommended that you use a fine-grained sudo configuration. For example, the following two lines in the /etc/sudoers
file allow the sally user to start and stop the db4e services.
sally ALL=(ALL) NOPASSWD: /bin/systemctl start db4e
sally ALL=(ALL) NOPASSWD: /bin/systemctl stop db4e
So a script that sally runs that uses this module will be successful in starting and stopping the db4e service, but will fail if enable()
or disable()
are attempted.
pip install db4e-systemd
Or clone locally:
git clone https://github.com/NadimGhaznavi/db4esystemd.git
cd db4e-systemd
from Db4eSystemd.Db4eSystemd import Db4eSystemd
svc = Db4eSystemd('db4e')
if not svc.installed():
print("Service not installed")
elif not svc.active():
print("Service is stopped. Starting...")
svc.start()
else:
print(f"Service is running with PID {svc.pid()}")
svc = Db4eSystemd('myservice')
svc.start() # Start service
svc.stop() # Stop service
svc.restart() # Restart service
svc.enable() # Enable service startup at boot time
svc.disable() # Disable service startup at boot time
svc.status() # Refresh status
svc.active() # True/False
svc.enabled() # True/False
svc.installed() # True/False
svc.pid() # Integer PID or None
svc.stdout() # Raw systemctl stdout
svc.stderr() # Raw systemctl stderr
GPL v3 - See LICENSE.txt
Created and maintained by Nadim-Daniel Ghaznavi. Part of the Database 4 Everything project.