Skip to content

Instantly share code, notes, and snippets.

@jseparovic
Created January 26, 2023 01:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jseparovic/c54ffbca986d07e57610bbf43c126350 to your computer and use it in GitHub Desktop.
Save jseparovic/c54ffbca986d07e57610bbf43c126350 to your computer and use it in GitHub Desktop.
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.
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment