Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up a systemd service to retry 5 times on a cycle of 30 seconds

I want systemd to start a script and retry a maximum of 5 times, 30s apart. Reading the systemd.service manual and searching the Internet didn't produce any obvious answers.

like image 902
jross Avatar asked Sep 02 '16 04:09

jross


People also ask

Will systemd restart service?

Systemd makes it very easy to restart a unit when it fails. Sometimes, this is all you really need.

What is StartLimitBurst?

StartLimitInterval=, StartLimitBurst= Configure service start rate limiting. By default, services which are started more than 5 times within 10 seconds are not permitted to start any more times until the 10 second interval ends. With these two options, this rate limiting may be modified.

How do you delay start of service in systemd?

Delay the start of the systemd service at boot. Inspect service that you want to delay at boot. redis-server. service - Advanced key-value store Loaded: loaded (/lib/systemd/system/redis-server.


1 Answers

After much trial and error I solved my problem and thought it worth posting here...

To allow a maximum of 5 retries separated by 30 seconds use the following options in the relevant systemd service file.

[Unit] StartLimitInterval=200 StartLimitBurst=5 [Service] Restart=always RestartSec=30 

This worked for me for a service that runs a script using 'Type=idle'. Note that 'StartLimitInterval' must be greater than 'RestartSec * StartLimitBurst' otherwise the service will be restarted indefinitely.

like image 128
jross Avatar answered Sep 27 '22 00:09

jross