1

Is there a setting in systemd to restart a service automatically after e.g. 24 hours?

Or what is the best workaround to implement such a function?

Background: I have a difficult-to-identify flaw in my long-running web-harvesting and storing-into-DB code. It does work for 10 or 20 days, then it is suddenly halting, until we reset it manually. Even excessive logging has given me no idea yet where exactly the problem is coming from. Of course, I would rather like to solve the underlying problem, but (for economic reasons) a brute-force kill-and-restart would probably also do the trick.


EDIT: I seem to have finally found the real flaw - an internet query for which I did not (could not) set a timeout (because the read_json function did not allow that option). Have now solved it via socket.setdefaulttimeout(...) - and I hope that will solve that infrequent halting problem itself. Thanks for your help!

AltSheets
  • 357
  • 1
  • 3
  • 10

2 Answers2

1

I guess your requirement is same as in this following question. question on periodic restart of service file

I hope this answers your question.

Community
  • 1
  • 1
Saturn
  • 966
  • 7
  • 14
  • Thanks a million. "WatchdogSec" = yes that is even easier than regularly notifying the watchdog "I am still alive". Will use that _next time_ ... because I seem to have finally found the real flaw - an internet query for which I could not set a timeout, because the read_json function did not allow that option. Have now solved it via socket.setdefaulttimeout(...) - and I hope that will solve that infrequent halting problem itself. But thanks thanks thanks for your help! – AltSheets Sep 25 '16 at 12:29
0

Systemd has a built-in watchdog function, see this link for more info.

However you will have to patch your software to send out sd_notify events, so the watchdog knows that your software is still alive.

From 1:

First of all, to make software watchdog-supervisable it needs to be patched to send out "I am alive" signals in regular intervals in its event loop. Patching this is relatively easy. First, a daemon needs to read the WATCHDOG_USEC= environment variable. If it is set, it will contain the watchdog interval in usec formatted as ASCII text string, as it is configured for the service. The daemon should then issue sd_notify("WATCHDOG=1") calls every half of that interval. A daemon patched this way should transparently support watchdog functionality by checking whether the environment variable is set and honouring the value it is set to.

An even more quick and dirty approach would be to set up a cron-job which kills the process every 24 hours and restarts your service.

data cosmos
  • 313
  • 3
  • 14
  • Wow that is really great. Thanks a million. "watchdog", fantastic. Will use it _next time_ ... because I seem to have finally found the real flaw - an internet query for which I could not set a timeout, because the read_json function did not allow that option. Have now solved it via socket.setdefaulttimeout(...) - and I hope that will solve that infrequent halting problem itself. But thanks thanks thanks for your help! – AltSheets Sep 25 '16 at 12:27