Systems engineer in business casual attire walking through

Fixing a Linux Service Stuck in a systemd Restart Loop

A systemd service restart loop starts when a service fails, and systemd repeatedly tries to start it. If the attempts happen too quickly, systemd stops and reports “start request repeated too quickly.”

That message explains why systemd stopped trying, but it doesn’t explain the first service failure. To find the cause, look at the error that appeared before the restart attempts filled the logs. At Basic Linux, we check that earlier error before changing restart settings.

This guide explains:

  • Why systemd stops restarting
  • How to read systemctl status and journalctl output
  • How to fix the usual causes before choosing a restart policy

Start by understanding why systemd keeps restarting a failing service and what makes it finally give up.

Why Does a systemd Service Keep Restarting Until It Fails?

System administrator hunched at a desk in a basement server room

A systemd service keeps restarting when its unit file tells systemd to try again after a failure. The setting is usually Restart=on-failure or Restart=always. If the same fault stops the process within seconds, systemd starts another attempt. The attempts stop when the service reaches its start limit.

Practically, systemd’s built-in defaults allow five starts within 10 seconds, though an administrator can change those values. The settings are StartLimitBurst= and StartLimitIntervalSec=. Once the service reaches the limit, systemd blocks further starts and marks the unit as failed.

Seen that way, the “systemctl service failed” state is systemd protecting the server from a process that burns CPU and floods the journal. So if you’ve chased these loops on Ubuntu and Debian servers, you’ll learn that the cause tends to show up in the first moments of startup.

Those early faults come from a short list of triggers. Common ones include a missing binary, a bad config file, a port already in use, wrong permissions, or a full disk. Since each attempt fails the same way, restarting harder never helps.

Reading the Evidence: What systemctl status and journalctl Errors Reveal

Once systemd stops restarting a service, check systemctl status for its exit code and recent log lines. Then use journalctl to find the error from the first failed start. Later attempts often repeat the same fault, so the earliest error gives you a place to investigate.

Below, we’ve explained five checks, moving from the quickest summary through to the unit file itself.

1. Start With systemctl status for the Exit Code and Attempt Count

Run sudo systemctl status myapp.service first, replacing myapp with your service name. The output shows the unit’s state, recent journal lines and, when a process failed, its exit status. sudo may reveal journal lines your user account can’t access, but status still shows only recent entries.

Then, look for a line like status=203/EXEC. Here, code 203/EXEC means systemd couldn’t run the configured program. And code 217/USER points to a problem setting up the service’s user credentials, which can include a missing account.

The restart counter message often sits just outside this short output. You should note the exit status, then move to journalctl for the full history.

2. Follow the Journal With journalctl -u to Find the First Error

For the full history, runjournalctl -u myapp.service. You can also add -e to jump to the end, then scroll up to where the first crash began.

Once you find the crash attempt, read its error before looking at later failures. Later attempts may report problems caused by the first failure. Copy the first error line exactly, since searching that text often finds the fix in the software’s documentation or GitHub issues.

In our experience, a crashed app leaving a stale lock file explains plenty of loops that look mysterious.

3. Filter journalctl by Boot and Priority to Cut the Noise

Filtering by boot and priority removes most of the clutter: add -b for the current boot and -p err for error-level messages only. Together, journalctl -u myapp -b -p err drops the startup chatter and keeps the failures.

This filter suits a busy Linux server where a service writes thousands of info lines. Switch to -b -1 when the loop started before the last reboot.

On top of that, --since "10 min ago" limits output to your latest tests. You’ll only see what changed after each fix (handy when you’re restarting the service every couple of minutes).

4. Run the ExecStart Command by Hand as the Service User

The journal may point to a startup error without showing what the program reports in a terminal. Find the ExecStart= command in the unit file and check that the service isn’t already running. Then run the command as the user named inUser=, for example, sudo -u www-data /opt/myapp/bin/server. The program may print an error directly in your terminal.

When the command fails in your terminal, its error may point to a missing file, incorrect permissions or a bad path. A manual test may also reveal a missing environment variable or a relative path that depends on the working directory. If nothing prints, check the app’s own log file, since some programs write there instead of to the terminal.

If the command runs by hand but fails under systemd, compare the service’s WorkingDirectory= and environment settings with your test. Add a required variable through Environment= or EnvironmentFile= only after you confirm what the program needs.

5. Check the Unit File Itself With systemctl cat

Running systemctl cat myapp.service prints the unit file plus any drop-in overrides in one view. A drop-in under /etc/systemd/system/myapp.service.d/ may change a setting you saw in the main file.

Read ExecStart=, User=, WorkingDirectory= and Type= first, and compare each with the error from the journal. For example, Type=forking tells systemd to wait for the program to fork, so a program that stays in the foreground may time out.

After you change a unit file or drop-in, run sudo systemctl daemon-reload so systemd reads the new settings. Then restart the service and check its status.

Support engineer at a home office desk in soft natural daylight

Fixing the Usual Culprits Behind a systemd Service Restart Loop

With the first error line in hand, match its message to the setting or resource the service needs. That gives you a specific fault to check before you change the unit file.

In practice, a “Permission denied” message points to an access problem, while 203/EXEC means systemd couldn’t run the configured program. So check the program’s path and execute permission first. The service user also needs access to its config file and permission to write where the app stores data. Our guide to reading and fixing Linux file permissions covers chmod and chown.

Resource problems look different in the log. For example, “address already in use” means another process holds the port, and sudo ss -tlnp names it. Meanwhile, “no space left on device” points to a full disk that leaves the operating system no room to write, which df -h confirms in seconds.

Use the error you found to choose a fix:

  • Program Path: Make sure ExecStart= points to the intended program, then use ls -l to inspect its execute permission. A 203/EXEC code usually traces back here.
  • Service User: Confirm the account named in User= exists. If it doesn’t, correct the setting or create the intended account.
  • Port Clash: An “address already in use” message means another process holds the port. Identify it withsudo ss -tlnp, then stop that process or move one service to another port.
  • Config Syntax: Run the app’s own config test and correct the error it reports. For Nginx, use nginx -t.
  • Full Disk: A “no space left on device” message points to a full filesystem, which df -h confirms it. Inspect what occupies the space before removing files the service may need.

Once you’ve fixed the cause, run sudo systemctl restart myapp.service and watch sudo journalctl -fu myapp.service for new errors. Plus, check systemctl status again to confirm the service stays active. If another error appears, read its message before changing anything else.

The Right Restart Policy Keeps a Fixed Service From Looping Again

After you fix the startup fault, check how the service should respond to a future failure. Restart= Controls when systemd tries again, while the start limit caps repeated attempts. Together, those settings let a service recover from an occasional failure without restarting indefinitely.

To pick the right setting for your service, compare the options in the table below.

Setting What it does Best suited to
Restart=no Never restarts the service One-shot scripts and scheduled jobs
Restart= on-failure Restarts after a non-zero exit, crash or timeout Most daemons and web apps
Restart=always Restarts after any exit, even a clean one Workers that should never stop
RestartSec=5s Waits five seconds between attempts Services that depend on a network or database

For a web app, Restart=on-failure and RestartSec=5s give the service five seconds before another attempt. That delay may help when a dependency briefly drops out. It doesn’t guarantee recovery if the database or network remains unavailable. With RestartSec=5s, five starts take at least 25 seconds, so raise StartLimitIntervalSec= to 60 seconds or the default 10-second window will never catch a loop.

Tired on-call engineer at night in a home office

Get the Service Stable and Keep It That Way

Before you change a restart setting, save the first error line and the exit code somewhere you can find them again. Once the service runs for a full day without a restart, review its Restart= and start-limit values so the next failure stops at a sensible point.

Basic Linux shares plain-English guides for people running their own Linux servers. Contact us to suggest a topic or ask about a service that still won’t stay up.

Leave a Reply

Your email address will not be published. Required fields are marked *