2

I have a simple systemd service unit to restart my python script on unconditional exits so defined Restart=always in the service definition. I just want to absolutely make sure even if the script hangs in some way, I can restart it by enabling the WatchdogSec= variable in the unit file.

My problem is the official docs for systemd's journal does not explain it clearly.

Quoting from the same page,

WatchdogSec=

Configures the watchdog timeout for a service. The watchdog is activated when the start-up is completed. The service must call sd_notify(3) regularly with "WATCHDOG=1" (i.e. the "keep-alive ping").

The part that is unclear is, will the service itself take care of maintaining the pings with the watchdog service (or) the application it is running (in my case - Python script) needs to maintain the ping by updating the environment variable mentioned.

My simple service definition

[Unit]
Description=dev-Status: Computes RF device availability status
           
[Service]
Type=simple

EnvironmentFile=/etc/sysconfig/db_EndPoint
ExecStart=/usr/bin/python /opt/foobar/foobar.py
RestartSec=10
Restart=always
         
[Install]
WantedBy=default.target

So to this service file if I just add the below line, would it restart my script automatically?

WatchdogSec=30s
2
  • Check out the original description here and the sd_notify man page.
    – meuh
    Nov 29, 2017 at 17:10
  • @meuh : Forgive my ignorance on the subject. Could you post an answer on what you are referring here?
    – Inian
    Nov 29, 2017 at 17:56

1 Answer 1

4

To "pat" or ping the watchdog, you can use the python systemd package to send the notification:

from systemd.daemon import notify, Notification
notify(Notification.WATCHDOG)
3
  • It worked real fine! Appreciate it, but can you point me what are the differences between these two similar python modules, the one you shared for systemd and python-systemd? They seem to have similar APIs built-in
    – Inian
    Nov 30, 2017 at 8:57
  • The mosquito version uses Cython to generate the binding, whereas the systemd version uses a CPython C module. Nov 30, 2017 at 16:51
  • FWIW python-systemd this didn't work for me on Raspberry Pi OS; had to use (sdnotify)
    – ijoseph
    Aug 29, 2020 at 23:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.