Killing orphaned WebSocket connections
The project I am working on uses nodejs and WebSockets. From time to time I kill my server and my clients in ways that leave connections still open. Eventually I find that new clients don't connect to new server instances. What to do?
The answer, discovered a while ago, lost, then found again is simple reference [here](http://stackoverflow.com/questions/750604/freeing-up-a-tcp-ip-port)
fuser <port>/tcp -k
Simple as that.
Well, maybe not that simple. Turns out you can still have zombies and things hanging around. In which case the magic formula is:
netstat -lp --tcp
That will give you a list of the outstanding TCP connections. Something like this will appear:
```(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address State PID/Program name
tcp 0 0 localhost:41087 *:* LISTEN 6955/svc_router
tcp 0 0 *:6060 *:* LISTEN 6950/mpythonrun
tcp 0 0 localhost:54000 *:* LISTEN 2093/plugin_host
tcp 0 0 awesome-mint *:* LISTEN -
tcp 0 0 localhost:ipp *:* LISTEN -
tcp 0 0 *:2009 *:* LISTEN 3299/lic_lm
```
The magic is the PID. Find the thing that's hanging onto the port you want and
kill -9 <PID>
The answer, discovered a while ago, lost, then found again is simple reference [here](http://stackoverflow.com/questions/750604/freeing-up-a-tcp-ip-port)
fuser <port>/tcp -k
Simple as that.
Well, maybe not that simple. Turns out you can still have zombies and things hanging around. In which case the magic formula is:
netstat -lp --tcp
That will give you a list of the outstanding TCP connections. Something like this will appear:
```(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address State PID/Program name
tcp 0 0 localhost:41087 *:* LISTEN 6955/svc_router
tcp 0 0 *:6060 *:* LISTEN 6950/mpythonrun
tcp 0 0 localhost:54000 *:* LISTEN 2093/plugin_host
tcp 0 0 awesome-mint *:* LISTEN -
tcp 0 0 localhost:ipp *:* LISTEN -
tcp 0 0 *:2009 *:* LISTEN 3299/lic_lm
```
The magic is the PID. Find the thing that's hanging onto the port you want and
kill -9 <PID>
Comments
Post a Comment