Skip to content

Instantly share code, notes, and snippets.

@sunsongxp
Created April 26, 2022 09:47
Show Gist options
  • Save sunsongxp/7b5467f9bd3278a5cf6f1425819f904b to your computer and use it in GitHub Desktop.
Save sunsongxp/7b5467f9bd3278a5cf6f1425819f904b to your computer and use it in GitHub Desktop.

Revisions

  1. Clownfused created this gist Jul 30, 2018.
    49 changes: 49 additions & 0 deletions systemd_service_restart_forever.service
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    # This systemd service configuration will ensure that your service will
    # attempt to restart your service FOREVER instead of the systemd default
    # of 5 failed retries within 10 seconds and then giving up forever.

    [Unit]
    Description=MY AMAZING SERVICE
    After=multi-user.target

    # NOTES ABOUT THE StartLimitBurst OPTION:
    # systemd default is StartLimitBurst=5 (give up after 5 failed attempts)
    # if you do NOT set set this option systemd will attempt
    # to restart your service forever.

    # NOTES ABOUT THE StartLimitIntervalSec OPTION:
    # systemd default is set StartLimitIntervalSec=10 - Set it to 0
    # and systemd will attempt to restart your service forever.
    StartLimitIntervalSec=0

    [Service]
    Type=simple
    WorkingDirectory=/home/user/path
    ExecStart=/usr/bin/python3 /home/user/path/example.py

    # NOTES ABOUT THE Restart=always OPTION:
    # By default, when you configure Restart=always systemd gives up
    # restarting your service if it fails to start more than 5 times
    # within a 10 seconds interval. Forever.
    # There are two [Unit] configuration options responsible for this
    # StartLimitBurst=5 and StartLimitIntervalSec=10

    # The simple fix that always works is to:
    # Set StartLimitIntervalSec=0 in [Unit] and do NOT add StartLimitBurst=5 in [Unit]
    # This way, systemd will attempt to restart your service FOREVER.
    Restart=always

    # NOTES ABOUT THE RestartSec OPTION:
    # By default, systemd attempts a restart after 100ms.
    # If you set RestartSec to restart after 3 seconds (RestartSec=3) then
    # the systemd default of [Unit] StartLimitBurst=5 and StartLimitIntervalSec=10 will never
    # have a chance to execute their tasks (reach 5 failed retries within 10 seconds)

    # The simple fix that always works is to:
    # Set StartLimitIntervalSec=0 in [Unit] and do NOT add StartLimitBurst=5 in [Unit] (As mentioned and explained above)
    # Then set RestartSec=1 - You should to set RestartSec to at least 1 second
    # to avoid putting too much stress on your server (if) when things start going wrong.
    RestartSec=1

    [Install]
    WantedBy=multi-user.target