SnowOps Night Shift

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

  1. journalctl -u orders-api -n 8 --no-pager

    The service's log, newest last. For a process that exits at start, the last lines before each restart say why it gave up.

  2. ls -l /opt/orders

    The 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.

  3. systemctl status orders-api --no-pager

    Shows 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.

  4. ls /opt/orders/releases

    Every release the deploy job has copied to this server, one folder each. The newest name is usually the one a deploy was switching to.

  5. tail -n 5 /var/log/orders-deploy.log

    The 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-api

How 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