Fix systemd service restart loop in 2 minutes
Your systemd service keeps restarting over and over. Here's how to stop the loop and fix the root cause fast.
You're staring at systemctl status and the service keeps flipping between active (running) and failed every few seconds. I've been there—it's like the machine is mocking you. Let's break the cycle.
Immediate fix: reset the unit and check journal
First, stop the loop manually so you can breathe:
- Run
sudo systemctl stop your-service-name. Replaceyour-service-namewith the actual unit (likenginxormyapp). - Now reset the failed state:
sudo systemctl reset-failed your-service-name. After that, you should seesystemctl status your-service-nameshowinactive (dead)—no more looping. - Check the logs to see what's crashing:
sudo journalctl -u your-service-name --since "5 minutes ago". Scroll to the bottom. Look for the last few lines beforeMain process exited. That's your smoking gun.
If the logs show exit code 1 or a segfault, the app itself is dying. If you see start-limit-hit, systemd's throttling kicked in after repeated failures.
Why it loops: the StartLimitInterval trap
By default, systemd gives a service 5 attempts within 10 seconds. If it keeps failing, systemd hits a StartLimitIntervalSec limit and stops trying. But here's the kicker: if your unit file has Restart=always or Restart=on-failure (which is common for daemons), systemd restarts immediately after each crash. That creates the loop you see.
The fix isn't changing Restart—it's fixing whatever's killing the process. But I'll show you how to adjust the limits too, in case the service needs more time to start.
Step-by-step permanent fix
- Open the service unit file:
sudo systemctl edit --full your-service-name. This creates an override in/etc/systemd/system/. - Add these lines inside the
[Service]section (or adjust existing ones):[Service] Restart=on-failure RestartSec=5 StartLimitIntervalSec=0RestartSec=5adds a 5-second delay between restarts—stops the frantic loop.StartLimitIntervalSec=0disables the restart limit entirely (no throttling). - Save the file, then run
sudo systemctl daemon-reload. After that, start the service:sudo systemctl start your-service-name. - Wait 30 seconds and check
systemctl status your-service-name. It should stayactive (running)or fail cleanly once—no more cycling.
But don't stop here. If the service still crashes repeatedly, you're masking the real problem. Go back to the journalctl output and fix the application error.
Less common variations
These edge cases bite people regularly:
- Timeout loop: Service fails to start within
TimeoutStartSec(default 90 seconds). Systemd kills it, restart kicks in, repeat. Fix by addingTimeoutStartSec=300in the[Service]section if the app is slow to initialize. - Dependency loop: Service requires another unit that also keeps failing. Check
journalctl -xefordependency failedmessages. Fix the dependency first, then restart both. - PID file corruption: Service writes a stale PID file, systemd thinks it's already running, fails to start, restarts. Add
PIDFile=/var/run/your-service.pidandExecStartPre=/bin/rm -f /var/run/your-service.pidto clean it each time. - Resource exhaustion: Service leaks memory or file handles, hits system limits (
LimitNOFILE,MemoryMax), crashes. Usesystemctl show your-service-name | grep -i limitto see current caps. AddLimitNOFILE=65536orMemoryMax=512Min the[Service]section.
Prevention: don't let it happen again
Three things to set early:
- Always test with
Restart=on-failure— avoidalwaysunless you really want it to restart on normal exit. It causes loops when you're debugging. - Set a sane
RestartSec— I use 2-5 seconds for most services. Prevents rapid-fire restarts from flooding the logs. - Monitor with
systemd-tmpfilesor a cron job— checksystemctl list-units --state=faileddaily. A loop that happened once might come back after a reboot.
That's it. The loop is broken, the logs are checked, and you've got a config that won't repeat the madness. Deploy the same pattern across your fleet and save yourself the headache later.
Was this solution helpful?