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?

guettli
  • 25,042
  • 81
  • 346
  • 663
  • Possibly related: https://www.freedesktop.org/software/systemd/python-systemd/daemon.html http://stackoverflow.com/questions/13069634/python-daemon-and-systemd-service – Robᵩ Apr 27 '16 at 15:22
  • @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. – guettli May 02 '16 at 11:38

3 Answers3

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.

user3159253
  • 16,836
  • 3
  • 30
  • 56
0

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

droid192
  • 2,011
  • 26
  • 43
0

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.

Joshua Shew
  • 618
  • 2
  • 19