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

  1. [root@Centos projects]# netstat -anp | grep 8087
  2. tcp6 0 0 :::8087 :::* LISTEN 4931/java
  3. tcp6 1 0 192.168.229.140:8087 192.168.229.1:10664 CLOSE_WAIT 4931/java
  4. tcp6 1 0 192.168.229.140:8087 192.168.229.1:10665 CLOSE_WAIT 4931/java
  5. tcp6 1 0 192.168.229.140:8087 192.168.229.1:10671 CLOSE_WAIT 4931/java
  6. tcp6 1 0 192.168.229.140:8087 192.168.229.1:10668 CLOSE_WAIT 4931/java
  7. tcp6 1 0 192.168.229.140:8087 192.168.229.1:10670 CLOSE_WAIT 4931/java
  8. [root@Centos projects]#

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

  1. [root@Centos projects]# lsof -np 4931 | grep "CLOSE_WAIT"
  2. java 4931 root 27u IPv6 75515 0t0 TCP 192.168.229.140:simplifymedia->192.168.229.1:10764 (CLOSE_WAIT)
  3. java 4931 root 28u IPv6 75516 0t0 TCP 192.168.229.140:simplifymedia->192.168.229.1:10765 (CLOSE_WAIT)
  4. java 4931 root 30u IPv6 75517 0t0 TCP 192.168.229.140:simplifymedia->192.168.229.1:10768 (CLOSE_WAIT)
  5. java 4931 root 31u IPv6 75518 0t0 TCP 192.168.229.140:simplifymedia->192.168.229.1:10769 (CLOSE_WAIT)
  6. java 4931 root 33u IPv6 75183 0t0 TCP 192.168.229.140:simplifymedia->192.168.229.1:10763 (CLOSE_WAIT)
  7. [root@Centos projects]# lsof -np 4931 | grep "CLOSE_WAIT" | awk \'{print $4}\'
  8. 27u
  9. 28u
  10. 30u
  11. 31u
  12. 33u
  13. [root@Centos projects]#

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

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

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

  1. (gdb) call close(27u)  # 27u即 close_wait 状态连接的文件描述符
  2. ...(略去内容)...

 

版权声明:本文为d0usr原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/d0usr/p/11836502.html