SnowOps Night Shift

How do I find which process is using a port on Linux when my service says Address already in use?

Only one process can listen on a TCP port at a time, so a second one fails with Address already in use. sudo ss -ltnp shows each listening socket with the process that owns it, and systemctl status on that process's service shows where it came from. A stopped service starts again at boot if it is still enabled. In this incident a retired export service came back after a reboot and held port 8080.

What it looks like

  • The service fails with Address already in use.
  • Requests get answers from the wrong program, such as 404 pages.
  • ss shows another process on the port.
  • It started after a reboot.

Commands that find the cause

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

    The service's log, newest last. A server that cannot start says why on its way out, often with an error number from the operating system.

  2. sudo ss -ltnp 'sport = :8080'

    Lists listening TCP sockets on port 8080 and, with sudo, the process that owns each one. Only one process can listen on a port at a time.

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

    Shows whether the service is running. A service that failed to start, or is restarting, shows that state and the reason in its last log lines.

  4. systemctl list-units --type=service --state=running --no-pager | grep -E 'orders|export'

    The services systemd is running now, filtered to order and export names. A service you thought was retired can still be here.

  5. curl -s -o /dev/null -w '%{http_code}\n' localhost:8080/export

    One request the way the warehouse makes it, printing only the HTTP status. 200 is a good export; anything else came from whatever answers on the port.

What the evidence looked like

Recorded on a real system when this incident was captured for the game.

$ journalctl -u orders-api -n 5 --no-pager
Nov 29 20:25:46 ops-1 python3[3110]:   File "/usr/lib/python3.12/socketserver.py", line 473, in server_bind
Nov 29 20:25:46 ops-1 python3[3110]:     self.socket.bind(self.server_address)
Nov 29 20:25:46 ops-1 python3[3110]: OSError: [Errno 98] Address already in use
Nov 29 20:25:46 ops-1 systemd[1]: orders-api.service: Main process exited, code=exited, status=1/FAILURE
Nov 29 20:25:46 ops-1 systemd[1]: orders-api.service: Failed with result 'exit-code'.

Root cause

The reboot started the still-enabled legacy-export service on port 8080 first; disabling it let orders-api bind.

The fix

Stop and disable the old service, then restart orders-api:

sudo systemctl disable --now legacy-export && sudo systemctl restart orders-api

How to prevent it

Retire a service with systemctl disable --now so it cannot return at boot, and check the port is free before starting the replacement.

More incident guides