Systemd - always have a service running and reboot if service stops more than X times
Asked Answered
B

2

8

I need to have a systemd service which runs continuously. System under question is an embedded linux built by Yocto. If the service stops for any reason (either failure or just completed), it should be restarted automatically If restarted more than X times, system should reboot.

What options are there for having this? I can think of the following two, but both seem suboptimal 1) having a cron job which will literally do the check above and keep the number of retries somewhere in /tmp or other tmpfs 2) having the service itself track the number times it has been started (again in some tmpfs location) and rebooting if necessary. Systemd would just have to continuously try to start the service if it's not running

edit: as suggested by an answer, I modified the service to use the StartLimitAction as given below. It causes the unit to correctly restart, but at no point does it reboot the system, even if I continuously kill the script:

[Unit]
Description=myservice system

[Service]
Type=simple
WorkingDirectory=/home/root
ExecStart=/home/root/start_script.sh

Restart=always
StartLimitAction=reboot
StartLimitIntervalSec=600
StartLimitBurst=5

[Install]
WantedBy=multi-user.target
Bravissimo answered 28/1, 2017 at 10:24 Comment(0)
N
6

This in your service file should do something very close to your requirements:

[Service]
Restart=always

[Unit]
StartLimitAction=reboot
StartLimitIntervalSec=60
StartLimitBurst=5

It will restart the service if it stops, except if there are more than 5 restarts in 60 seconds: in that case it will reboot.

You may also want to look at WatchdogSec value, but this software watchdog functionality requires support from the service itself (very easy to add though, see the documentation for WatchDogSec).

Nofretete answered 28/1, 2017 at 12:51 Comment(1)
It doesn't seem to work - I've tried killing the script a dozen times and it does indeed restart the script, but it never reboots the system. I'm probably missing something. Added the systemd unit in the original questionBravissimo
G
1

My understanding is that the line Restart= should be in [Service], as in the example

but lines StartLimitxxxxx= should be in [Unit].

Gallantry answered 16/1, 2021 at 14:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.