Skip to main content

sd_notify and sd_watchdog_enabled functionality for writing Python daemons under systemd

Project description

systemd_watchdog

sd_notify(3) and sd_watchdog_enabled(3) client functionality implemented in Python 3 for writing Python daemons

Install

$ pip install systemd-watchdog

or

$ git clone ...
$ make install

Usage

import systemd_watchdog

wd = systemd_watchdog.watchdog()
if not wd.enabled():
    # Then it's probably not running is systemd with watchdog enabled
    raise Exception("Watchdog not enabled")

# Report a status message
wd.status("Starting my service...")
time.sleep(3)

# Report that the program init is complete
wd.ready()
wd.status("Waiting for web requests...")
wd.notify()
time.sleep(3)

# Compute time between notifications
timeout_half_sec = int(float(wd.timeout) / 2e6)  # Convert us->s and half that
time.sleep(timeout_half_sec)
wd.notify()

# Report an error to the service manager
wd.notify_error("An irrecoverable error occured!")
# The service manager will probably kill the program here
time.sleep(3)

Public Interface

systemd_watchdog.watchdog - commonly used properties and methods

ping

The only method required for the simplest implementation; combines notify_due with notify() to only send "alive" notifications at reasonable intervals.

Returns boolean indicating if a message was sent or not.

beat

Alias for ping() if you prefer heartbeat terminology.

ready()

Report ready service state, i.e. completed init (only needed with Type=notify).

status(msg)

Send a service status message.

systemd_watchdog.watchdog - less-used properties and methods

is_enabled

Boolean property stating whether watchdog capability is enabled.

timeout

Property reporting the number of microseconds (int) before process will be killed.

It is recommended that you call notify() once roughly half of this interval has passed (see notify_due).

timeout_td

Property that is the same as timeout but presented as datetime.timedelta for easier manipulation.

notify_due

Boolean property indicating more than half of the watchdog interval has passed since last update.

notify()

Report a healthy service state. Other calls, e.g. status() do not reset the watchdog.

notify_error(msg=None)

Report an error to the watchdog manager. This program will likely be killed upon receipt.

If msg is provided, it will be reported as a status message prior to the error.

History

Aaron D. Marasco May 2020

License

See LICENSE file

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

systemd-watchdog-0.9.0.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

systemd_watchdog-0.9.0-py2.py3-none-any.whl (4.8 kB view details)

Uploaded Python 2Python 3

File details

Details for the file systemd-watchdog-0.9.0.tar.gz.

File metadata

  • Download URL: systemd-watchdog-0.9.0.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2

File hashes

Hashes for systemd-watchdog-0.9.0.tar.gz
Algorithm Hash digest
SHA256 cacc10cea46603b2ad20dcba5ead2f745232a1737c24356debcef0d0d7a9d20e
MD5 325be5cf464ce56ca29e0e01f618a328
BLAKE2b-256 3007c63ccff9b3b4e801d5573cb543a4ffed82ce4ee657de974f16a182abe670

See more details on using hashes here.

File details

Details for the file systemd_watchdog-0.9.0-py2.py3-none-any.whl.

File metadata

  • Download URL: systemd_watchdog-0.9.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2

File hashes

Hashes for systemd_watchdog-0.9.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 902f96b33c2d02a3a0e8a0c36ad3a78f0903ac57bdcddb9d0d898a62e023a50b
MD5 a24fceff11e441ac27ce39a1060ceaf8
BLAKE2b-256 d797cf9179a9a86ac2d13bfebb94da3a00ab78fe91191c2b81bffdb432cf4f42

See more details on using hashes here.

Supported by

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