Why does my systemd service keep restarting right after a deploy?
systemd restarts a service that exits, so a program that fails at start turns into a restart loop. systemctl status shows the state and the command it runs, and journalctl -u shows the error from each attempt. When the error names a file that cannot be opened, check that path on disk. In this incident a deploy removed the current release symlink and failed before creating the new one.
What it looks like
- systemctl status shows activating (auto-restart).
- The journal repeats the same error on every attempt.
- The error names a path that does not exist.
- A deploy ran or failed shortly before.
Commands that find the cause
journalctl -u orders-api -n 8 --no-pagerThe service's log, newest last. For a process that exits at start, the last lines before each restart say why it gave up.
ls -l /opt/ordersThe order API's folder. In a long listing, a symbolic link shows as name -> target; a link that is missing simply is not in the list.
systemctl status orders-api --no-pagerShows the service state, the command it runs (ExecStart), the main process and recent logs. activating (auto-restart) with a status code means it exits and systemd keeps starting it again.
ls /opt/orders/releasesEvery release the deploy job has copied to this server, one folder each. The newest name is usually the one a deploy was switching to.
tail -n 5 /var/log/orders-deploy.logThe deploy job's own log on this server, newest last. Each step it ran is one line, so a deploy that stopped halfway shows where.
What the evidence looked like
Recorded on a real system when this incident was captured for the game.
$ journalctl -u orders-api -n 8 --no-pager
Nov 29 20:23:48 ops-1 python3[2175]: /usr/bin/python3: can't open file '/opt/orders/current/app.py': [Errno 2] No such file or directory
Nov 29 20:23:48 ops-1 systemd[1]: orders-api.service: Main process exited, code=exited, status=2/INVALIDARGUMENT
Nov 29 20:23:48 ops-1 systemd[1]: orders-api.service: Failed with result 'exit-code'.
Nov 29 20:23:51 ops-1 systemd[1]: orders-api.service: Scheduled restart job, restart counter is at 12.
Nov 29 20:23:51 ops-1 systemd[1]: Started orders-api.service - SnowOps order export API.
Nov 29 20:23:51 ops-1 python3[2189]: /usr/bin/python3: can't open file '/opt/orders/current/app.py': [Errno 2] No such file or directory
Nov 29 20:23:51 ops-1 systemd[1]: orders-api.service: Main process exited, code=exited, status=2/INVALIDARGUMENT
Nov 29 20:23:51 ops-1 systemd[1]: orders-api.service: Failed with result 'exit-code'.Root cause
The r42 deploy removed /opt/orders/current and failed before relinking it; pointing it at r42 fixed the API.
The fix
Point current at the release the deploy was switching to, then restart:
sudo ln -sfn /opt/orders/releases/r42 /opt/orders/current && sudo systemctl restart orders-apiHow to prevent it
Switch release links in one atomic step (ln -sfn to a temporary name, then rename), and make the deploy check the service is healthy before it reports success.
More incident guides
- Linux disk full: no space left on device
- Address already in use: find what holds a port on Linux
- Linux name or service not known after a config change
- Too many open files in a systemd service
- CrashLoopBackOff after a config change
- Service returns 503 while every pod is Running
- OOMKilled and exit code 137
- Pods fail to start after a Secret change
- Requests fail after adding a NetworkPolicy
- Healthy pods restarted by a liveness probe
- Slow service with low CPU and no restarts
- Rollout stuck on ImagePullBackOff
- Pods stuck Pending with insufficient CPU
- Docker container exits immediately after a config change
- Docker port mapping not working, empty reply
- Docker container cannot connect to the database host
- Docker volume permission denied
- Docker container exit code 137, killed for memory