Systemd Daemon in Python with watchdog support
Asked Answered
H

4

6

I want to write a daemon in python which gets started via systemd.

I want to use Type=notify, this way I don't have to do the double fork magic.

According to the docs:

The reference implementation for this notification is provided by libsystemd-daemon.so

... how to do this with Python?

Horner answered 27/4, 2016 at 15:12 Comment(2)
Possibly related: freedesktop.org/software/systemd/python-systemd/daemon.html #13070134Protection
@Robᵩ The question you mention does not solve my question, since my question is about a daemon with watchdog support. If my daemon hangs in an endless loop, I want systemd to know that the service does not respond. Without watchdog systemd things my service is alive as long as the process runs.Horner
S
10

Probably, you could use sdnotify python module which is a pure-python implementation of sd_notify protocol. Actually, the protocol is rather simple, so the module implementation is quite short.

To use watchdog machinery you should add WatchdocSec=<smth> to the unit file, and then send WATCHDOG=1 messages on a regular basis from your service. Check Restart= option as well.

Selfreliant answered 9/5, 2016 at 8:57 Comment(2)
Nice, the package can be installed via pip. Thank you.Horner
It's how I've discovered it a while ago :). pip search facility is rather handy.Selfreliant
P
1

use the package
https://pypi.org/project/systemd-python/
it is the offical systemd devs and maintained.

Pillion answered 25/4, 2020 at 11:55 Comment(1)
You didn't answer the question. Give an example of how to notify systemd from python using that package.Dade
C
1

You can use the systemd-python package. Specifically, consider the notify function in the daemon module.

from systemd.daemon import notify
notify("WATCHDOG=1")

According to the docs this will...

Send a message to the init system about a status change.

Centralism answered 16/8, 2023 at 20:55 Comment(0)
G
0

I implemented the functionality in systemd-watchdog-thread

Githens answered 18/11, 2023 at 13:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.