Python dbus interface library for systemd
Uses dbus_python to interface with systemd's dbus API.
- Start/Stop/Restart/Reload services
- Enable/Disable service files
- Mask/Unmask service files
- Get service state/error code
- Get service/service file properties
- Reload systemd daemon (to apply service file changes)
- New in 1.3.0 Subscribe to service property change events
- dbus
- libdbus-1-dev
- Python 3
- dbus-python
- context-logger
pip install .-
Create source distribution
pip setup.py sdist
-
Install from distribution file
pip install dist/systemd-dbus-1.0.0.tar.gz
-
Install from GitHub repository
pip install git+https://github.com/EffectiveRange/python-systemd-dbus.git@latest
Note: service_name is automatically appended with .service if not provided.
from dbus import SystemBus
from systemd_dbus import SystemdDbus
systemd = SystemdDbus(SystemBus())
systemd.start_service('service_name')
systemd.stop_service('service_name')
systemd.restart_service('service_name')
systemd.reload_service('service_name')from dbus import SystemBus
from systemd_dbus import SystemdDbus
systemd = SystemdDbus(SystemBus())
systemd.enable_service('service_name')
systemd.disable_service('service_name')from dbus import SystemBus
from systemd_dbus import SystemdDbus
systemd = SystemdDbus(SystemBus())
systemd.mask_service('service_name')
systemd.unmask_service('service_name')from dbus import SystemBus
from systemd_dbus import SystemdDbus
systemd = SystemdDbus(SystemBus())
state = systemd.get_active_state('service_name')
print(state)
error_code = systemd.get_error_code('service_name')
print(error_code)from dbus import SystemBus
from systemd_dbus import SystemdDbus
systemd = SystemdDbus(SystemBus())
properties = systemd.get_service_properties('service_name')
for key, value in properties.items():
print(f'{key}: {value}')
properties = systemd.get_service_file_properties('service_name')
for key, value in properties.items():
print(f'{key}: {value}')from dbus import SystemBus
from systemd_dbus import SystemdDbus
systemd = SystemdDbus(SystemBus())
state = systemd.reload_daemon()Subscribe to service property change events. Retrieve the object path of the service from the output of the following command:
dbus-send --system --print-reply --reply-timeout=2000 --type=method_call \
--dest=org.freedesktop.systemd1 /org/freedesktop/systemd1 org.freedesktop.systemd1.Manager.ListUnits | grep dhcpcd_Output:
object path "/org/freedesktop/systemd1/unit/dhcpcd_2eservice"Example to print status changes for dhcpcd service:
from dbus import SystemBus
from typing import Any
from systemd_dbus import SystemdDbus
def on_property_changed(*args: Any) -> None:
_, props, _ = args
state = props.get('ActiveState')
if state:
print(f'State of dhcpcd service changed to {state}')
systemd = SystemdDbus(SystemBus())
systemd.add_property_change_handler('/org/freedesktop/systemd1/unit/dhcpcd_2eservice', on_property_changed)