A backend service crashes at 03:00. systemd dutifully restarts it. It crashes again. systemd restarts it again. By the time anyone looks, the unit is in failed state because it hit StartLimitBurst an hour ago, the disk is full of core dumps, and the application log is the only place that recorded the original error — which has since been overwritten. None of this is a systemd bug. It is the default unit file doing exactly what it was asked to do.
What’s happening
A Restart=always line is not a production policy. systemd will happily loop a crashing process at maximum speed, hammer CPU, and burn through the start-limit window. Then it gives up and the service stays down. The defaults are tuned for a developer iterating locally, not a host that needs to absorb a transient fault and keep running.
The directives that change this are unglamorous and live in the [Service] and [Unit] sections. Restart=on-failure only restarts on non-zero exit, RestartSec= introduces backoff between attempts, and RestartPreventExitStatus= lets you mark exits that should be permanent (a config-validation failure, for instance — there is no point retrying that). Hardening directives like NoNewPrivileges=, ProtectSystem=, and PrivateTmp= cost almost nothing and remove entire classes of post-exploitation moves. LimitNOFILE= matters the moment your app server passes a few thousand concurrent sockets — the default is often 1024.
How to verify
Inspect the unit currently in use, including drop-ins:
systemctl cat myapp.service
systemctl show myapp.service | grep -E '^(Restart|LimitNOFILE|NoNewPrivileges|ProtectSystem|PrivateTmp)='
Read the actual exit history and restart count:
systemctl status myapp.service
journalctl -u myapp.service --since "1 hour ago" -o short-precise
Check the open-file ceiling the process is actually running with:
cat /proc/$(pidof myapp)/limits | grep -i 'open files'
If Max open files is 1024 and you are running an HTTP server, the next traffic spike will surface as accept: too many open files in the log.
The fix
Drop a file at /etc/systemd/system/myapp.service.d/override.conf rather than editing the vendor unit:
[Service]
Restart=on-failure
RestartSec=5s
RestartPreventExitStatus=78
LimitNOFILE=65536
NoNewPrivileges=yes
ProtectSystem=strict
PrivateTmp=yes
StandardOutput=journal
StandardError=journal
[Unit]
After=network-online.target postgresql.service
Wants=network-online.target
StartLimitIntervalSec=300
StartLimitBurst=5
Reload with systemctl daemon-reload && systemctl restart myapp.service. The unit now backs off cleanly on transient failures, refuses to retry config errors, keeps logs in journald where they survive a restart loop, and stops being a privilege-escalation amplifier.
Hardening every long-running service this way is part of how Stack Harbor runs Environment Management. If a service has been flapping and nobody is sure why, we can look at it.