Skip to main content

systemd wrapper in Cython

Project description

pypi version License

systemd wrapper in Cython

Python systemd wrapper using Cython.

Installation

About Binary Wheels Distribution

Historically, cysystemd was not distributed via wheels due to systemd versioning challenges. While the libsystemd headers remain relatively stable, the ABI can vary between different OS versions and distributions. Previous attempts to distribute wheels resulted in compatibility issues across different Linux systems. Currently, we use the manylinux_2_34 format for wheel distribution, which bundles the necessary shared objects (.so files) required for operation.

This approach should provide compatibility with modern systemd installations.

However, if you encounter any compatibility issues, we strongly recommend installing the package from source code instead.

pip install --no-binary=:all: cysystemd

Installation via Debian packages

Pre-built .deb packages are available for Debian/Ubuntu in the GitHub Releases page.

Installation from PyPI

Once the system dependencies are installed, you can install cysystemd:

pip install cysystemd

Installation from Source

You must install the systemd development headers (libsystemd) before installation! Without these headers, the installation will fail.

For Debian/Ubuntu users:

apt install build-essential libsystemd-dev

On older versions of Debian/Ubuntu, you might also need:

apt install libsystemd-daemon-dev libsystemd-journal-dev

For CentOS/RHEL:

yum install gcc systemd-devel

BREAKING CHANGES in v2.0.0

AsyncJournalReader Changes

  1. Major refactoring of the AsyncJournalReader iterator implementation:

    • Removed internal queue and threading-based implementation
    • Now uses direct async iteration through journal events
    • More reliable handling of journal invalidation events
    • Simpler and more efficient implementation
  2. wait() method now returns JournalEvent instead of boolean

    • Returns specific event type (JournalEvent.APPEND, JournalEvent.INVALIDATE, JournalEvent.NOP)
    • Better error handling and event processing

Type Annotations

  • Added comprehensive type hints throughout the codebase
  • Added return type annotations for all public methods
  • Enhanced IDE support and code documentation

API Behavior Changes

  • seek_tail() now automatically calls previous() to ensure cursor is positioned correctly
  • Improved error handling and validation in various methods
  • More consistent return types across the API

Python Support

  • Added support for Python 3.13
  • Maintained support for Python 3.8-3.12

Dependency Changes

  • Requires latest libsystemd development headers
  • Binary wheels are no longer distributed (see "Why binary wheels are no longer distributed" above)

Please ensure your code is updated to handle these changes when upgrading to version 2.0.0.

Usage examples

Writing to journald

Logging handler for python logger

from cysystemd import journal
import logging
import uuid

logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger()
logger.addHandler(journal.JournaldLogHandler())

try:
    logger.info("Trying to do something")
    raise Exception('foo')
except:
    logger.exception("Test Exception %s", 1)

systemd daemon notification

from cysystemd.daemon import notify, Notification

# Send READY=1
notify(Notification.READY)

# Send status
notify(Notification.STATUS, "I'm fine.")

# Send stopping
notify(Notification.STOPPING)

Write message into systemd journal:

from cysystemd import journal


journal.write("Hello Lennart")

# Or send structured data
journal.send(
    message="Hello Lennart",
    priority=journal.Priority.INFO,
    some_field='some value',
)

Reading journald

Reading all systemd records

from cysystemd.reader import JournalReader, JournalOpenMode

journal_reader = JournalReader()
journal_reader.open(JournalOpenMode.SYSTEM)
journal_reader.seek_head()

for record in journal_reader:
    print(record.data['MESSAGE'])

Read only cron logs

from cysystemd.reader import JournalReader, JournalOpenMode, Rule


rules = (
  Rule("SYSLOG_IDENTIFIER", "CRON") &
  Rule("_SYSTEMD_UNIT", "crond.service") |
  Rule("_SYSTEMD_UNIT", "cron.service")
)

cron_reader = JournalReader()
cron_reader.open(JournalOpenMode.SYSTEM)
cron_reader.seek_head()
cron_reader.add_filter(rules)

for record in cron_reader:
    print(record.data['MESSAGE'])

Polling records

from cysystemd.reader import JournalReader, JournalOpenMode


reader = JournalReader()
reader.open(JournalOpenMode.SYSTEM)
reader.seek_tail()

poll_timeout = 255

while True:
    reader.wait(poll_timeout)

    for record in reader:
       print(record.data['MESSAGE'])

journald open modes

  • CURRENT_USER
  • LOCAL_ONLY
  • RUNTIME_ONLY
  • SYSTEM
  • SYSTEM_ONLY - deprecated alias of SYSTEM
from cysystemd.reader import JournalReader, JournalOpenMode


reader = JournalReader()
reader.open(JournalOpenMode.CURRENT_USER)

journald entry

JournalEntry class has some special properties and methods:

  • data - journal entry content (dict)
  • date - entry timestamp (datetime instance)
  • cursor - systemd identification bytes for this entry
  • boot_id() - returns bootid
  • get_realtime_sec() - entry epoch (float)
  • get_realtime_usec() - entry epoch (int microseconds)
  • get_monotonic_sec() - entry monotonic time (float)
  • get_monotonic_usec() - entry monotonic time (int microseconds)
  • __getitem__(key) - shoutcut for entry.data[key]

journald reader

JournalReader class has some special properties and methods:

  • open(flags=JournalOpenMode.CURRENT_USER) - opening journald with selected mode
  • open_directory(path) - opening journald from path
  • open_files(*filename) - opening journald from files
  • data_threshold - may be used to get or set the data field size threshold for data returned by fething entry data.
  • closed - returns True when journal reader closed
  • locked - returns True when journal reader locked
  • idle - returns True when journal reader opened
  • seek_head - move reader pointer to the first entry
  • seek_tail - move reader pointer to the last entry
  • seek_monotonic_usec - seeks to the entry with the specified monotonic timestamp, i.e. CLOCK_MONOTONIC. Since monotonic time restarts on every reboot a boot ID needs to be specified as well.
  • seek_realtime_usec - seeks to the entry with the specified realtime (wallclock) timestamp, i.e. CLOCK_REALTIME. Note that the realtime clock is not necessarily monotonic. If a realtime timestamp is ambiguous, it is not defined which position is sought to.
  • seek_cursor - seeks to the entry located at the specified cursor (see JournalEntry.cursor).
  • wait(timeout) - It will synchronously wait until the journal gets changed. The maximum time this call sleeps may be controlled with the timeout_usec parameter.
  • __iter__ - returns JournalReader object
  • __next__ - calls next() or raise StopIteration
  • next(skip=0) - returns the next JournalEntry. The skip parameter skips some entries.
  • previous(skip=0) - returns the previous JournalEntry. The skip parameter skips some entries.
  • skip_next(skip) - skips next entries.
  • skip_previous(skip) - skips next entries.
  • add_filter(rule) - adding filter rule. See read-only-cron-logs_ as example.
  • clear_filter - reset all filters
  • fd - returns a special file descriptor
  • events - returns EPOLL events
  • timeout - returns internal timeout
  • process_events() - After each poll() wake-up process_events() needs to be called to process events. This call will also indicate what kind of change has been detected.
  • get_catalog() - retrieves a message catalog entry for the current journal entry. This will look up an entry in the message catalog by using the "MESSAGE_ID=" field of the current journal entry. Before returning the entry all journal field names in the catalog entry text enclosed in "@" will be replaced by the respective field values of the current entry. If a field name referenced in the message catalog entry does not exist, in the current journal entry, the "@" will be removed, but the field name otherwise left untouched.
  • get_catalog_for_message_id(message_id: UUID) - works similar to get_catalog() but the entry is looked up by the specified message ID (no open journal context is necessary for this), and no field substitution is performed.

Asyncio support

Initial asyncio support for reading journal asynchronously.

AsyncJournalReader

Blocking methods were wrapped by threads. Method wait() use epoll on journald file descriptor.

import asyncio
import json

from cysystemd.reader import JournalOpenMode
from cysystemd.async_reader import AsyncJournalReader


async def main():
    reader = AsyncJournalReader()
    await reader.open(JournalOpenMode.SYSTEM)
    await reader.seek_tail()

    async for record in reader:
        print(json.dumps(record.data, indent=1, sort_keys=True))

if __name__ == '__main__':
    asyncio.run(main())

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cysystemd-2.0.5.tar.gz (338.7 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

cysystemd-2.0.5-cp314-cp314-manylinux_2_34_x86_64.whl (2.4 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.34+ x86-64

cysystemd-2.0.5-cp314-cp314-manylinux_2_34_aarch64.whl (2.2 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.34+ ARM64

cysystemd-2.0.5-cp313-cp313-manylinux_2_34_x86_64.whl (2.4 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.34+ x86-64

cysystemd-2.0.5-cp313-cp313-manylinux_2_34_aarch64.whl (2.2 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.34+ ARM64

cysystemd-2.0.5-cp312-cp312-manylinux_2_34_x86_64.whl (2.4 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ x86-64

cysystemd-2.0.5-cp312-cp312-manylinux_2_34_aarch64.whl (2.3 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ ARM64

cysystemd-2.0.5-cp311-cp311-manylinux_2_34_x86_64.whl (2.4 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.34+ x86-64

cysystemd-2.0.5-cp311-cp311-manylinux_2_34_aarch64.whl (2.3 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.34+ ARM64

cysystemd-2.0.5-cp310-cp310-manylinux_2_34_x86_64.whl (2.4 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.34+ x86-64

cysystemd-2.0.5-cp310-cp310-manylinux_2_34_aarch64.whl (2.2 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.34+ ARM64

cysystemd-2.0.5-cp39-cp39-manylinux_2_34_x86_64.whl (2.4 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.34+ x86-64

cysystemd-2.0.5-cp39-cp39-manylinux_2_34_aarch64.whl (2.2 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.34+ ARM64

cysystemd-2.0.5-cp38-cp38-manylinux_2_34_x86_64.whl (2.4 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.34+ x86-64

cysystemd-2.0.5-cp38-cp38-manylinux_2_34_aarch64.whl (2.3 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.34+ ARM64

File details

Details for the file cysystemd-2.0.5.tar.gz.

File metadata

  • Download URL: cysystemd-2.0.5.tar.gz
  • Upload date:
  • Size: 338.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for cysystemd-2.0.5.tar.gz
Algorithm Hash digest
SHA256 faac7506b6a709e96c0b0028bc5b3b62c3f5457b31e13e7d82b7a5f1c1889d96
MD5 7b63d971ddf8ce1e3dee0c4c5b19a69a
BLAKE2b-256 11ca2451ce4dd2e9d9d770630486bdae5dd161cf10576e0bf7443a8b33d42a2f

See more details on using hashes here.

Provenance

The following attestation bundles were made for cysystemd-2.0.5.tar.gz:

Publisher: publish.yml on mosquito/cysystemd

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cysystemd-2.0.5-cp314-cp314-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for cysystemd-2.0.5-cp314-cp314-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 4597b258d7ea55ef6ff2471fbf6ac674b47432ed7b3009e061532bd2e73251d7
MD5 b63900a71cefdf6d824059fcea025ee3
BLAKE2b-256 af003c6419b2dc62b271e50dfcd52d2a25b83c8e47e792e49cc4c0eec8db8754

See more details on using hashes here.

Provenance

The following attestation bundles were made for cysystemd-2.0.5-cp314-cp314-manylinux_2_34_x86_64.whl:

Publisher: publish.yml on mosquito/cysystemd

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cysystemd-2.0.5-cp314-cp314-manylinux_2_34_aarch64.whl.

File metadata

File hashes

Hashes for cysystemd-2.0.5-cp314-cp314-manylinux_2_34_aarch64.whl
Algorithm Hash digest
SHA256 87b474cb6a3a4c6fea5e822c4c61a3dcdd913b5255bbd1d887677db68279ed91
MD5 efda899308151b326ba92db4d835247b
BLAKE2b-256 85bd3eb545685d68b46e9d694e6205ee5798b7a7c9093dd4e2059a2f04e80813

See more details on using hashes here.

Provenance

The following attestation bundles were made for cysystemd-2.0.5-cp314-cp314-manylinux_2_34_aarch64.whl:

Publisher: publish.yml on mosquito/cysystemd

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cysystemd-2.0.5-cp313-cp313-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for cysystemd-2.0.5-cp313-cp313-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 a8c573e6ec970b3643dfae675a904ef81aa59d78e8e9ff331896c293a4057279
MD5 ce84d0f7f9380ee0865957b5f4262ee0
BLAKE2b-256 997091cadc7439b8ecc2c35287226ba434eb19d82f1984d437d176429629dd37

See more details on using hashes here.

Provenance

The following attestation bundles were made for cysystemd-2.0.5-cp313-cp313-manylinux_2_34_x86_64.whl:

Publisher: publish.yml on mosquito/cysystemd

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cysystemd-2.0.5-cp313-cp313-manylinux_2_34_aarch64.whl.

File metadata

File hashes

Hashes for cysystemd-2.0.5-cp313-cp313-manylinux_2_34_aarch64.whl
Algorithm Hash digest
SHA256 9ec006a3549f1eef13f34e87bf62eb744cd0d588171c936e90d93cf3e3ac4ec3
MD5 af2eae7080e3a2e44f32678dbb42780e
BLAKE2b-256 199851677d2b07e8fcec04288c0a2fd3e76d4676930a5751e1ed3927037300a4

See more details on using hashes here.

Provenance

The following attestation bundles were made for cysystemd-2.0.5-cp313-cp313-manylinux_2_34_aarch64.whl:

Publisher: publish.yml on mosquito/cysystemd

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cysystemd-2.0.5-cp312-cp312-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for cysystemd-2.0.5-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 764568f622392a8cbc6c692ec83ce63baca559434f850ff3e90712d9275a3a8b
MD5 2ea54c57003a1f10af99f1c826aba7a2
BLAKE2b-256 4e1324676667869d60f07364d6ec10f68eb93da04df088273332d23ca0f81679

See more details on using hashes here.

Provenance

The following attestation bundles were made for cysystemd-2.0.5-cp312-cp312-manylinux_2_34_x86_64.whl:

Publisher: publish.yml on mosquito/cysystemd

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cysystemd-2.0.5-cp312-cp312-manylinux_2_34_aarch64.whl.

File metadata

File hashes

Hashes for cysystemd-2.0.5-cp312-cp312-manylinux_2_34_aarch64.whl
Algorithm Hash digest
SHA256 693426351795a82d0ce6b3c6667434c25eb671bc1652dd374bbf1144d4af7aa9
MD5 cbd5a378082f9892be3c113044523182
BLAKE2b-256 711edf65cdb1ce10ca651d22a05d9c64acae092df19eeb2b3e4e5aea1f9fb3b6

See more details on using hashes here.

Provenance

The following attestation bundles were made for cysystemd-2.0.5-cp312-cp312-manylinux_2_34_aarch64.whl:

Publisher: publish.yml on mosquito/cysystemd

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cysystemd-2.0.5-cp311-cp311-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for cysystemd-2.0.5-cp311-cp311-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 95f692ebdeb219ab770c7f638434acd76504e97beef6054c7ee37ad8847cb656
MD5 68203c0ecf7af4e9fd1beac643da1e90
BLAKE2b-256 ac1d68ca056f37b803603b7bd6e9c7f4b71c009460cd135af85021cef78922ab

See more details on using hashes here.

Provenance

The following attestation bundles were made for cysystemd-2.0.5-cp311-cp311-manylinux_2_34_x86_64.whl:

Publisher: publish.yml on mosquito/cysystemd

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cysystemd-2.0.5-cp311-cp311-manylinux_2_34_aarch64.whl.

File metadata

File hashes

Hashes for cysystemd-2.0.5-cp311-cp311-manylinux_2_34_aarch64.whl
Algorithm Hash digest
SHA256 f2e9479bd471a38ab8dfc6c52c9ba33462e7c37b64e50975ef0eaaf242d684c0
MD5 197238a352f67fdea012027cc4915cf3
BLAKE2b-256 bdabfa40178df49786e5b0e1f379a42d0255c90745fc3875d24fc11934eee603

See more details on using hashes here.

Provenance

The following attestation bundles were made for cysystemd-2.0.5-cp311-cp311-manylinux_2_34_aarch64.whl:

Publisher: publish.yml on mosquito/cysystemd

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cysystemd-2.0.5-cp310-cp310-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for cysystemd-2.0.5-cp310-cp310-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 515755c45bf605d9942c696afedc050a46468b2ea3d940394a64af462db1c87b
MD5 7d9920a50546736b14bef4e62aaf1a69
BLAKE2b-256 ef8e1b27d017b9eb6b2071d4faedbea724d3a6f29601a54a8857fcf3311f8e60

See more details on using hashes here.

Provenance

The following attestation bundles were made for cysystemd-2.0.5-cp310-cp310-manylinux_2_34_x86_64.whl:

Publisher: publish.yml on mosquito/cysystemd

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cysystemd-2.0.5-cp310-cp310-manylinux_2_34_aarch64.whl.

File metadata

File hashes

Hashes for cysystemd-2.0.5-cp310-cp310-manylinux_2_34_aarch64.whl
Algorithm Hash digest
SHA256 c9a34f408e06d1dc0f527ec0793c781357411489ad2d6b53406d966a77e52dc0
MD5 5b7cc37a4608325fb4ad485c1db0fa1c
BLAKE2b-256 ddcd5e68592e05a9b2d91f14664b3379aafb0cedd2fd733952fac9f8eb3a3b66

See more details on using hashes here.

Provenance

The following attestation bundles were made for cysystemd-2.0.5-cp310-cp310-manylinux_2_34_aarch64.whl:

Publisher: publish.yml on mosquito/cysystemd

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cysystemd-2.0.5-cp39-cp39-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for cysystemd-2.0.5-cp39-cp39-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 9024e53c3ca04d6c17fca0861e776b55c939f5efa7f9b0e573a2230a16345f5c
MD5 43497d1d12f1b6033c2830714ab9403c
BLAKE2b-256 efa5037182ec4bfd3c347fe8681331181c93784e2b9e518fd16d8de65c405728

See more details on using hashes here.

Provenance

The following attestation bundles were made for cysystemd-2.0.5-cp39-cp39-manylinux_2_34_x86_64.whl:

Publisher: publish.yml on mosquito/cysystemd

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cysystemd-2.0.5-cp39-cp39-manylinux_2_34_aarch64.whl.

File metadata

File hashes

Hashes for cysystemd-2.0.5-cp39-cp39-manylinux_2_34_aarch64.whl
Algorithm Hash digest
SHA256 d8ab460465654a668c2cb648b96b47be5f38f132299c69a8ea0eb471d0ba9e1f
MD5 fd7e98cef87e5ee9959a4dc0e0ff58ad
BLAKE2b-256 49c1c4d5a9dd95cbee0b233a1192fb3565e95aeb5c436ee6f2a45ddf11124cec

See more details on using hashes here.

Provenance

The following attestation bundles were made for cysystemd-2.0.5-cp39-cp39-manylinux_2_34_aarch64.whl:

Publisher: publish.yml on mosquito/cysystemd

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cysystemd-2.0.5-cp38-cp38-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for cysystemd-2.0.5-cp38-cp38-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 2dec07a4c86b45102c05569383d4eb47a5d80bf6318c1965236960f856cd7fa8
MD5 9a65958139dce10c61e23369e9287c3b
BLAKE2b-256 a478c694310c8c750f6c121d66f2b6c966cdebee1cec821b7ce1e82322a7fcdc

See more details on using hashes here.

Provenance

The following attestation bundles were made for cysystemd-2.0.5-cp38-cp38-manylinux_2_34_x86_64.whl:

Publisher: publish.yml on mosquito/cysystemd

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cysystemd-2.0.5-cp38-cp38-manylinux_2_34_aarch64.whl.

File metadata

File hashes

Hashes for cysystemd-2.0.5-cp38-cp38-manylinux_2_34_aarch64.whl
Algorithm Hash digest
SHA256 aca22edef17468341bb38eea6ca0f543d20aed2aaa8147cf1565ad5a2de2020f
MD5 c71156bc0e2bd549d7c098cb87ea4387
BLAKE2b-256 305020fba11d0d517f3b61bf0c05a22d60127baf5defd1039471c63e06e00331

See more details on using hashes here.

Provenance

The following attestation bundles were made for cysystemd-2.0.5-cp38-cp38-manylinux_2_34_aarch64.whl:

Publisher: publish.yml on mosquito/cysystemd

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page