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