/proc/pid/fd/
中,文件描述符过多。我可以使用shell命令关闭这些文件描述符吗?/proc/pid/fd/
中,文件描述符过多。我可以使用shell命令关闭这些文件描述符吗?只要您有权限,就可以关闭其他正在运行的进程的fd。
首先,找到PID。
然后,启动gdb并附加到该进程:
gdb -p 1598
然后,调用close系统调用来关闭你想要关闭的文件描述符:
(gdb) call close(999)
$1 = 0
如果文件描述符是一个泄露的,那么程序永远不会再次尝试使用它,并且不应该引起任何问题。然而,这个程序很可能有一个错误。你可以在Bash中通过以下方式关闭当前进程的文件描述符 n
:
exec n<&-
@Thomas的回答只有在安装了close()
调用的调试信息时才有效。
如果没有安装调试信息,gdb将拒绝调用close()
:
(gdb) call close(3)
'close' has unknown return type; cast the call to its declared return type
close()
的最简单方法是将调用强制转换为close()
返回类型:(gdb) call (int)close(3)
$1 = 0
Find your destination address and port:
netstat --program [ --numeric-host --numeric-ports ] | grep [<pid>]/[<appname>]
$ netstat --program --numeric-ports | grep 8812/
tcp 0 0 ysc.xxx:54055 10.56.1.152:30000 ESTABLISHED 8812/my-application
tcp 0 0 ysc.xxx:46786 postgres.xxx:5432 ESTABLISHED 8812/my-application
tcp 0 0 ysc.xxx:36090 10.56.4.79:57000 ESTABLISHED 8812/my-application
...
unix 2 [ ] DGRAM 7177020 8812/my-application
Here, I'd like to cut 10.56.4.79:57000
.
Create an iptables
rule to cut the socket:
iptables -A OUTPUT [ --out-interface <if> --protocol <tcp|udp|unix> ] --destination <addr> --dport <port> --jump DROP
$ iptables -A OUTPUT --destination 10.56.4.79 --dport 57000 --jump DROP
$
At this stage, your program can't send packets to the distant host. In most cases, the TCP connection is closed. You can proceed with your tests if there is some.
$ netstat --program --numeric-ports | grep 8812/
tcp 0 0 ysc.xxx:54055 10.56.1.152:30000 ESTABLISHED 8812/my-application
tcp 0 0 ysc.xxx:46786 postgres.xxx:5432 ESTABLISHED 8812/my-application
...
unix 2 [ ] DGRAM 7177020 8812/my-application
Remove the iptables
rule:
You just type in the same iptables
rule replacing the A
by a D
.
$ iptables -D OUTPUT --destination 10.56.4.79 --dport 57000 --jump DROP
$
你不能随意关闭其他进程的文件描述符并期望它们继续工作。
修复打开了太多文件的程序,使其打开较少的文件。这可能是配置更改或修改源代码等。你不能只是关闭它的文件。