{"id":10219,"library":"sdnotify","title":"sdnotify: Systemd Service Notification","description":"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.","status":"active","version":"0.3.2","language":"en","source_language":"en","source_url":"https://github.com/bb4242/sdnotify","tags":["systemd","service","notification","linux","daemon"],"install":[{"cmd":"pip install sdnotify","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"symbol":"SystemdNotifier","correct":"from sdnotify import SystemdNotifier"}],"quickstart":{"code":"import time\nfrom sdnotify import SystemdNotifier\n\ndef main():\n    notifier = SystemdNotifier()\n\n    print(\"Sending NOTIFY_SOCKET check (silently fails if not running under systemd)\")\n    # Send 'READY=1' to signal that the service is initialized\n    notifier.notify(\"READY=1\")\n    print(\"Service ready. Working...\")\n\n    for i in range(1, 4):\n        status_message = f\"STATUS=Working on task {i} of 3...\"\n        print(status_message)\n        notifier.notify(status_message)\n        time.sleep(2) # Simulate work\n\n    # Send 'STOPPING=1' or just exit (systemd can infer STOPPING upon exit)\n    print(\"Service finished work. Sending STOPPING=1.\")\n    notifier.notify(\"STOPPING=1\")\n\nif __name__ == \"__main__\":\n    main()","lang":"python","description":"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."},"warnings":[{"fix":"Ensure your systemd unit file is configured with `Type=notify` and that the service is started correctly by systemd, allowing `NOTIFY_SOCKET` to be inherited by your Python process.","message":"Notifications are silently ignored if the `NOTIFY_SOCKET` environment variable is not set. This means your Python service is likely not running under systemd, or systemd is not configured to listen for notifications (e.g., `Type=notify` is missing in the unit file).","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always send `notifier.notify('READY=1')` once your service is fully initialized and ready to accept requests. This is crucial for proper service lifecycle management by systemd.","message":"Systemd expects a `READY=1` notification to mark the service as successfully started. Forgetting to send this can cause systemd to prematurely consider the service failed or stalled, leading to restarts or timeouts.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Upgrade to `0.3.0` or later to ensure stable and working functionality with the standard systemd notification protocol.","message":"Versions prior to `0.3.0` were considered experimental and may have been non-functional or had an unstable API. The `sdnotify` functionality became reliably available starting with `0.3.0`.","severity":"breaking","affected_versions":"<0.3.0"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Install the package using pip: `pip install sdnotify`.","cause":"The `sdnotify` library is not installed in the Python environment being used to run your service.","error":"ModuleNotFoundError: No module named 'sdnotify'"},{"fix":"In your `.service` unit file, ensure `Type=notify` is set. In your Python code, confirm that `notifier.notify('READY=1')` is called after all initialization steps are complete.","cause":"This often happens if the systemd unit file's `Type` is not set to `notify`, or if the Python service fails to send `READY=1` to systemd upon startup.","error":"Systemd service unit reports 'failed' or 'timeout' even though Python script appears to run."}]}