Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close a file descriptor from another process in unix systems

You can use command lsof to get file descriptors for all running processes, but what I would like to do is to close some of those descriptors without being inside that process. This can be done on Windows, so you can easily unblock some application.

Is there any command or function for that?

like image 327
Seb Avatar asked Nov 27 '08 07:11

Seb


People also ask

How do I close a file descriptor?

close() closes a file descriptor, so that it no longer refers to any file and may be reused. Any record locks (see fcntl(2)) held on the file it was associated with, and owned by the process, are removed (regardless of the file descriptor that was used to obtain the lock).

How do you pass file descriptors between processes?

Named streams are useful for passing file descriptors between unrelated processes on the same machine. A user process can send a file descriptor to another process by invoking the I_SENDFD ioctl(2) on one end of a named stream.

How do you close a file in Unix?

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 force close a file in Linux?

Use "pgrep" and "pkill" Linux Force Kill Commands As with the kill command, this should close the Linux process within around 5 seconds.


1 Answers

I don't know why you are trying to do this, but you should be able to attach to the process using gdb and then call close() on the fd. Example:

In one shell: cat

In another shell:

$pidof cat 7213  $gdb -p 7213  ... lots of output ...  (gdb) 

Now you tell gdb to execute close(0):

(gdb) p close(0)  $1 = 0  (gdb) c  Continuing.  Program exited with code 01. (gdb) 

In the first shell I get this output:

cat: -: Bad file descriptor  cat: closing standard input: Bad file descriptor 
like image 149
Andreas Avatar answered Sep 28 '22 03:09

Andreas