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 狀態連接的文件描述符 ...(略去內容)...