在线关闭 CLOSE_WAIT状态TCP连接

1、查看某个端口的所有TCP连接:

[root@Centos projects]# netstat -anp | grep 8087
tcp6       0      0 :::8087                 :::*                    LISTEN      4931/java           
tcp6       1      0 192.168.229.140:8087    192.168.229.1:10664     CLOSE_WAIT  4931/java           
tcp6       1      0 192.168.229.140:8087    192.168.229.1:10665     CLOSE_WAIT  4931/java           
tcp6       1      0 192.168.229.140:8087    192.168.229.1:10671     CLOSE_WAIT  4931/java           
tcp6       1      0 192.168.229.140:8087    192.168.229.1:10668     CLOSE_WAIT  4931/java           
tcp6       1      0 192.168.229.140:8087    192.168.229.1:10670     CLOSE_WAIT  4931/java           
[root@Centos projects]# 

2、获取 CLOSE_WAIT 状态连接的文件描述符:

[root@Centos projects]# lsof -np 4931 | grep "CLOSE_WAIT"
java    4931 root   27u     IPv6              75515       0t0      TCP 192.168.229.140:simplifymedia->192.168.229.1:10764 (CLOSE_WAIT)
java    4931 root   28u     IPv6              75516       0t0      TCP 192.168.229.140:simplifymedia->192.168.229.1:10765 (CLOSE_WAIT)
java    4931 root   30u     IPv6              75517       0t0      TCP 192.168.229.140:simplifymedia->192.168.229.1:10768 (CLOSE_WAIT)
java    4931 root   31u     IPv6              75518       0t0      TCP 192.168.229.140:simplifymedia->192.168.229.1:10769 (CLOSE_WAIT)
java    4931 root   33u     IPv6              75183       0t0      TCP 192.168.229.140:simplifymedia->192.168.229.1:10763 (CLOSE_WAIT)
[root@Centos projects]# lsof -np 4931 | grep "CLOSE_WAIT" | awk '{print $4}'
27u
28u
30u
31u
33u
[root@Centos projects]# 

3、使用GDB关闭 CLOSE_WAIT状态连接:

[root@Centos projects]# gdb -p 4931  # 连接到 4931 进程
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-94.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Attaching to process 4931
...(略去内容)...
(gdb)           # 这里为 gdb 命令提示符

然后根据文件描述符关闭指定的 socket 连接:

(gdb) call close(27u)  # 27u即 close_wait 状态连接的文件描述符
...(略去内容)...
艺无止境,诚惶诚恐, 感谢开源贡献者的努力!!
原文地址:https://www.cnblogs.com/d0usr/p/11836502.html