Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close file descriptor via Linux shell command

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

like image 612
Eric Avatar asked May 13 '11 06:05

Eric


People also ask

How do I close file descriptor?

close() closes a file descriptor, so that it no longer refers to any file and may be reused.

How do you close a file in Linux?

Additionally, you can use shortcut methods. Press the [Esc] key and type Shift + Z Z to save and exit or type Shift+ Z Q to exit without saving the changes made to the file.

How do you close a file in Linux bash?

Example 01: Using Exit 0 The first method we have been utilizing in this example is to use the “exit” statement in the bash script. Create a new file in the shell with the help of a “touch” command and open it in any editor. The read statement is widely known to get input from the user.

How do you close a file in Unix shell script?

It is CTRL V CTRL M. windows/Linux or UNIX/mac.

Can I use shell command to close a file descriptor?

Can I use shell command to close these file descriptors? Show activity on this post. You can definitely close fd's of other running processes as long as you have the permissions to do so. First, find the PID. 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.

How do I open a duplicate file descriptor in Linux?

In addition, programmers can open, close, or duplicate file descriptors with the exec built-in command and the I/O redirection operator: Duplicate the file descriptor from $src to $dst. Both $dst and $src will refer to the file which was referred by $src.

How do I know if a file descriptor is closed?

If word evaluates to '-', file descriptor n, or standard output if n is not specified, is closed. Attempts to close a file descriptor that is not open shall not constitute an error. If word evaluates to something else, the behavior is unspecified.

What to do if a file descriptor is leaking?

First, find the PID. 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. Show activity on this post. You can close a FD n of the current process in bash as so:


1 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.

like image 104
Thomas Vander Stichele Avatar answered Nov 03 '22 03:11

Thomas Vander Stichele