Search code examples
linuxsystemd

How to restart Systemd service only once on-failure?


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.


Solution

  • 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.