lsof
search : find process for socket, list open files and sockets
I used to use netstat to find the process that owned a socket port :
Result :
It seems it yields the same result as root. Just as lsof :
lsof seems shorter and easier :
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
backend 3832228 kees 3u IPv4 30243139 0t0 TCP *:6666 (LISTEN)
Also you can print all open files of that process with
Note that here sudo may be handy to suppress some warnings about files it cannot 'stat'.
problem with Unable to bind: Address already in use
This test run sheds a light on the problem. Note that we already tried the socket.SO_REUSEADDR setting. But just after a crash when you restart the server it will say address in use.
If we use lsof to find the port in question it seems like the calling side is the one holding the port.
kees@hoek:~/projects/aivrp/broker$ lsof -i :7777
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
worker 3683252 kees 3u IPv6 63643637 0t0 TCP hoek:37212->hoek:7777 (CLOSE_WAIT)
So it is worker that hangs around, not broker which listens on port 7777. And indeed when we stop the worker debugger session, the port is freed :