HowTo: Kill TCP Connections in CLOSE_WAIT State

If you are seeing a large number of connections persisting in CLOSE_WAIT state, it’s probably a problem with the application itself.

Restarting it will clear the connections temporarily, but obviously, further investigation will be required to find the cause of the problem.

If restarting of application is undesirable, you can manually kill all connections that are in CLOSE_WAIT state.

Kill CLOSE_WAIT connections by IP

Kill TCP connections in CLOSE_WAIT state, established with the foreign IP address

$ netstat -anp |\
grep |\
grep CLOSE_WAIT |\
awk '{print $7}' |\
cut -d \/ -f1 |\
grep -oE "[

7 Replies to “HowTo: Kill TCP Connections in CLOSE_WAIT State”

  1. I meet this problem, but My program is close, the close_wait state has existed, do you have some way to close this connections

  2. Thanks for this. It helped me.
    Another script which can work is :

    lsof -i :80 |grep CLOSE_WAIT| awk '{print $2}|uniq| xargs kill
  3. In my case, the pid is of application only, so killing it will kill the application. How to handle that?

  4. This is ridiculous, all this to ultimately kill the application 😀

  5. In some cases there are no PID’s and it is showing as blank, what to do in this scenario?

  6. If your process is running and you don’t want to kill the process but just the fd then use gdb. Get the pid and fd info from lsof and execute this.
    # gdb -p –batch -ex ‘call close()’

Leave a Reply