7

Is it possible to restart a systemd service when there is no output generated for a predefined amount of time?

I have a script which apparently can hang, but that is not detected by systemd (or Python for that case), and thus it does not enter the failed state.

However, it does stop logging output, so I should be able to restart the service after no output is given for a minute. Is this possible with systemd?

My current systemd file:

[Unit]
Description=SOmething
After=network.target

[Service]
WorkingDirectory=/home/user/system/something
User=nobody
ExecStart=/usr/bin/python2 something.py
Restart=on-watchdog
RestartSec=10s

[Install]
WantedBy=multi-user.target
2
  • Does your app write output to any log file? Or just to stdout for systemd to report on? – Stephen Harris Jul 25 '16 at 0:24
  • Currently it is only outputting to stdout, but that would of course be something that can be changed. – Bob Van de Vijver Jul 25 '16 at 6:49
2

I don't think systemd allows you to do that, at least the systemd.service(5) manual page doesn't seem to mention anything like that.

However, what you could do is use systemd's builtin watchdog.

You would do that by settings WatchdogSec= and then having your service send WATCHDOG=1 with sd_notify regularly. If you set WatchdogSec=30 then your service must notify systemd at most every 30 seconds.

When your service hangs, it won't notify systemd anymore, and systemd will kill your service as a result.

With restart=on-watchdog (which you already have, but it doesn't do anything without WatchdogSec=), then systemd will restart your service after it's been killed by the watchdog.

1
  • Although this requires me to adjust the Python-code (it is not my own), it is a good solution for my problem. Thank you for your extensive answer! – Bob Van de Vijver Jul 25 '16 at 7:05

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.