1

I have a service that should restart only once when something go wrong. Here is my service:

[Unit]
Description=Do job on boot
AllowIsolate=yes
StartLimitBurst=1    # or 2
StartLimitIntervalSec=3

[Service]
User=root
Type=idle

ExecStart=/usr/bin/python3 /var/www/flask/initJobs.py
Restart=on-failure
RestartSec=3

[Install]
WantedBy=multi-user.target

But this on failure keep restarting.

systemd version 241.

Tried to add StartLimitAction=none but did not change nothing.

Zorro
  • 1,085
  • 12
  • 19
  • Hi @Zorro, based on what I know it is not possible. The interval has to be limited in time.... But I could be as long as 100years ...which is close to unlimited right ? Would that work for you to only allow one retry every 100 year ? if so : https://www.freedesktop.org/software/systemd/man/systemd.unit.html#StartLimitIntervalSec=interval and this https://stackoverflow.com/questions/39284563/how-to-set-up-a-systemd-service-to-retry-5-times-on-a-cycle-of-30-seconds could help. – Paulo Sep 28 '21 at 15:26
  • Good trick! Can works if `RestartSec` is counted after the first restart. And won't work if i need 2 restarts. I think it could be possible to abord after n restart. – Zorro Sep 28 '21 at 15:34
  • I think you can make it work for any number of retries. – Paulo Sep 28 '21 at 21:01
  • `StartLimitIntervalSec=100y, StartLimitBurst=1` would only trigger one restart `StartLimitIntervalSec=100y, StartLimitBurst=10` would tigger 10 – Paulo Sep 28 '21 at 21:05
  • using StartLimitIntervalSec=100y and StartLimitBurst=1 on first restart i got: `Failed to start Job Service` `myJob.service: Start request repeated too quickly` – Zorro Sep 28 '21 at 21:29
  • Try to increase the StartLimitBurst .... The first start must count has one burst. – Paulo Sep 28 '21 at 21:35

1 Answers1

2

Ok i found, so to restart service only once on failure after 3 second.

[Unit]
Description=Do job on boot
StartLimitBurst=2
StartLimitInterval=11          # StartLimitInterval >  RestartSec * StartLimitBurst
#OR
#StartLimitIntervalSec=11  

[Service]
User=root
Type=idle

ExecStart=/usr/bin/python3 /var/www/flask/initJobs.py
Restart=on-failure
RestartSec=3

[Install]
WantedBy=multi-user.target

the key here is StartLimitIntervalSec have to be > to RestartSec * StartLimitBurst and StartLimitBurst count the first start.

Zorro
  • 1,085
  • 12
  • 19