sdnotify: Systemd Service Notification

0.3.2 · active · verified Fri Apr 17

sdnotify provides a pure Python implementation of systemd's service notification protocol (sd_notify). It allows Python services to communicate their status, readiness, and other information back to systemd, enabling robust service management. The current version is 0.3.2, with releases being infrequent, primarily for packaging or minor internal improvements.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize `SystemdNotifier` and send status messages, including `READY=1` and `STATUS` updates, to systemd. This script should be run as a `Type=notify` service under systemd to observe the notifications.

import time
from sdnotify import SystemdNotifier

def main():
    notifier = SystemdNotifier()

    print("Sending NOTIFY_SOCKET check (silently fails if not running under systemd)")
    # Send 'READY=1' to signal that the service is initialized
    notifier.notify("READY=1")
    print("Service ready. Working...")

    for i in range(1, 4):
        status_message = f"STATUS=Working on task {i} of 3..."
        print(status_message)
        notifier.notify(status_message)
        time.sleep(2) # Simulate work

    # Send 'STOPPING=1' or just exit (systemd can infer STOPPING upon exit)
    print("Service finished work. Sending STOPPING=1.")
    notifier.notify("STOPPING=1")

if __name__ == "__main__":
    main()

view raw JSON →