Tcpkill is the command (comes with package dsniff)
I have used it to terminate a web-socket connection to a particular worker process, but this can be used to terminate any TCP connection including one on the loopback interface.
Here’s how to identify the active web socket connections nginx has to my backend worker process:
$ sudo netstat -anpo |grep ESTABLISHED | grep 2020
tcp 0 0 127.0.0.1:2020 127.0.0.1:60895 ESTABLISHED 18567/python off (0.00/0/0)
tcp 0 0 127.0.0.1:60895 127.0.0.1:2020 ESTABLISHED 15997/nginx: worker off (0.00/0/0)
And here’s the command line to kill the tcp connection:
$ sudo tcpkill -i lo port 60895
tcpkill: listening on lo [port 60895]
127.0.0.1:60895 > 127.0.0.1:2020: R 1232933003:1232933003(0) win 0
127.0.0.1:60895 > 127.0.0.1:2020: R 1232933259:1232933259(0) win 0
127.0.0.1:60895 > 127.0.0.1:2020: R 1232933771:1232933771(0) win 0
127.0.0.1:2020 > 127.0.0.1:60895: R 1218906507:1218906507(0) win 0
127.0.0.1:2020 > 127.0.0.1:60895: R 1218906763:1218906763(0) win 0
127.0.0.1:2020 > 127.0.0.1:60895: R 1218907275:1218907275(0) win 0
127.0.0.1:2020 > 127.0.0.1:60895: R 1218906507:1218906507(0) win 0
127.0.0.1:2020 > 127.0.0.1:60895: R 1218906763:1218906763(0) win 0
127.0.0.1:2020 > 127.0.0.1:60895: R 1218907275:1218907275(0) win 0
^C
Very handy for testing reconnect logic within the application.
NOTE: For tcpkill to work there needs to be some traffic flowing through the connection, so you can start the command then try sending some data.