cysystemd

raw JSON →
2.0.5 verified Fri May 01 auth: no python

A Python library providing Cython-based bindings for systemd journal and socket activation. Current version 2.0.5, supporting Python 3.8–3.12. Released under MIT license.

pip install cysystemd
error ImportError: No module named 'systemd'
cause Using old import path from version <2.0.0.
fix
Change import to from cysystemd.journal import ...
error OSError: [Errno 2] No such file or directory: '/run/systemd/journal/socket'
cause Running on a system without systemd or the journal socket is missing.
fix
Ensure systemd is running and journald is active.
error AttributeError: module 'cysystemd.journal' has no attribute 'sd_journal_sendv'
cause sd_journal_sendv was removed in a recent version.
fix
Use journal_send() instead.
gotcha Journal.get_next() returns None when no more entries. Code must handle this.
fix Always check if entry is not None before accessing.
breaking In version 2.0.0, the module structure changed. Old imports from 'systemd.journal' no longer work.
fix Use from cysystemd.journal import ... instead of from systemd.journal import ...
gotcha The library requires systemd runtime (e.g., /run/systemd/journal/socket). Importing on non-Linux or without systemd raises ImportError.
fix Wrap imports in try/except ImportError or run only on systemd-based systems.
deprecated sd_journal_sendv() is deprecated in favor of journal_send().
fix Use journal_send() instead.

Send and read systemd journal messages.

import os
from cysystemd.journal import Journal, journal_send

# Send a message to systemd journal
journal_send("Hello from cysystemd", PRIORITY=6, SYSLOG_IDENTIFIER="myapp")

# Read from journal
with Journal() as journal:
    journal.seek_tail()
    journal.previous()
    entry = journal.get_next()
    print(entry.get('MESSAGE', 'No message'))