Skip to content

EffectiveRange/python-systemd-dbus

Repository files navigation

CI Coverage badge

python-systemd-dbus

Python dbus interface library for systemd

Uses dbus_python to interface with systemd's dbus API.

Table of Contents

Features

  • 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

Requirements

Debian packages

  • dbus
  • libdbus-1-dev
  • Python 3

Python packages

  • dbus-python
  • context-logger

Installation

Install from source root directory

pip install .

Install from source distribution

  1. Create source distribution

    pip setup.py sdist
  2. Install from distribution file

    pip install dist/systemd-dbus-1.0.0.tar.gz
  3. Install from GitHub repository

    pip install git+https://github.com/EffectiveRange/python-systemd-dbus.git@latest

Usage

Note: service_name is automatically appended with .service if not provided.

Start/Stop/Restart/Reload services

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')

Enable/Disable service files

from dbus import SystemBus
from systemd_dbus import SystemdDbus

systemd = SystemdDbus(SystemBus())

systemd.enable_service('service_name')
systemd.disable_service('service_name')

Mask/Unmask service files

from dbus import SystemBus
from systemd_dbus import SystemdDbus

systemd = SystemdDbus(SystemBus())

systemd.mask_service('service_name')
systemd.unmask_service('service_name')

Get service state/error code

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)

Get service/service file properties

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}')

Reload systemd daemon

from dbus import SystemBus
from systemd_dbus import SystemdDbus

systemd = SystemdDbus(SystemBus())

state = systemd.reload_daemon()

New in 1.3.0

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)

About

Python dbus interface library for systemd

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages