How to close file descriptor via Linux shell command

Eric picture Eric · May 13, 2011 · Viewed 36.6k times · Source

In /proc/pid/fd/, there are too many file descriptors. Can I use shell command to close these file descriptors?

Answer

Thomas Vander Stichele picture Thomas Vander Stichele · Aug 21, 2012

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.