In /proc/pid/fd/
, there are too many file descriptors. Can I use shell command to close these file descriptors?
-
Which process is it? Is it your program? Can you post some source?– MarkRMay 14, 2011 at 6:53
-
I had a case where some commercial app opened the same file with more than 1000 file descriptors, then ran out of file descriptors. The application was even unable to terminate though its own commands, so I had to kill it. If I were able to close some of the file descriptors, the program might have been able to terminate more cleanly.– U. WindlFeb 6, 2019 at 14:56
5 Answers
You can definitely close fd's of other running processes as long as you have the permissions to do so.
First, find the PID.
Then, start gdb and attach to the process:
gdb -p 1598
Then, call the close system call on the fd you want to close:
(gdb) call close(999)
$1 = 0
If the file descriptor was a leaked one, then the program will never try to use it again anyway, and it shouldn't cause any issues. The program most likely has a bug, however.
You can close a FD n
of the current process in bash as so:
exec n<&-
-
3Thanks. But it is used to close FD of the current process (bash process). Do you know any method to close FD of specific process?– EricMay 13, 2011 at 8:07
@Thomas answer is valid only when debug information for close()
call is installed.
Without debug info installed, gdb refuses to call close()
:
(gdb) call close(3)
'close' has unknown return type; cast the call to its declared return type
The simplest way to make gdb call close()
in this case is to cast the call to close()
return type:
(gdb) call (int)close(3)
$1 = 0
See gdb documentation:
Sometimes, a function you wish to call is missing debug information. In such case, GDB does not know the type of the function, including the types of the function’s parameters. To avoid calling the inferior function incorrectly, which could result in the called function functioning erroneously and even crash, GDB refuses to call the function unless you tell it the type of the function.
For prototyped (i.e. ANSI/ISO style) functions, there are two ways to do that. The simplest is to cast the call to the function’s declared return type.
-
Probably due to aggressive modern symbol table stripping: close() being a system call, it should be known to any process that uses the shared C library.– U. WindlFeb 6, 2019 at 14:54
I've ran in a similar situation, but where gdb
was not an option since it disrupted the real-time constraints of my application and distorted my test.
So I came up with a quick iptables
rule. Optional arguments put into square brackets ([ opt ]
).
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 theA
by aD
.$ iptables -D OUTPUT --destination 10.56.4.79 --dport 57000 --jump DROP $
-
Note that my intention was to test how my application would react if it lost its connection to a critical service.– YSCFeb 23, 2016 at 8:38
-
The question was generic to file descriptors; your answer is for network connections only. And most of all, it does not close the file descriptor; it just drops the network traffic.– U. WindlFeb 6, 2019 at 14:52
-
@U.Windl If your application behaves correctly, this leads to the end of that TCP connection and of the file descriptor. As I said in my answer "In most cases, the TCP connection is closed." It is not a panacea, but if it can help people...– YSCFeb 6, 2019 at 15:31
-
Only if "keepalive" option is being used in TCP: Otherwise dead connections can stay there until closed or being reset.– U. WindlFeb 11, 2019 at 8:01
-
1This may not have been an answer to the question asked exactly, but it helped me do what I was trying to do. Apr 1, 2019 at 0:42
You can't just go around closing other processes' file descriptors and expect them to keep working.
Fix the program which has too many files open to make it open fewer. This may be a config change, or modifying the source etc. You can't just close the files for it.
-
11Sure you can. A common case for wanting to do this is because the program leaks fd's; in which case it will never use those again. Sure, it's a bug, but it's still valid to want to work around it. Aug 21, 2012 at 15:10
-
In my case, I had a long-running hung batch process because a read from an http socket had died. Closing the fd as @ThomasVanderStichele described solved the problem for me :-) Mar 31, 2015 at 0:45
-
1