As +vonbrand pointed out you can just use /proc/<pid>/fd/<fd>
. But you can not just "write there" and it will not give any firworks.
It is a special symbolic link to the file that is opened in process with pid <pid>
as file descriptor <fd>
.
Just use it to open the exact same file in your process.
You do not have to worry about the original file beeing deleted or replaced because using this link will always give you the original file the process opened. Just try the following small piece of bash code:
#!/bin/bash
echo "test" >/tmp/file
ls -li /tmp/file
exec 3<> /tmp/file
rm /tmp/file
ls -lLi /proc/$$/fd/3
cat /proc/$$/fd/3
This creates a file /tmp/file
containing the string test. Opens the file as file descriptor 3, removes it and after removing it can still cat
its content by using /proc/self/fd/3
. In linux a file is not finally deleted as long as any process still uses it.
So instead of getting and using the file descriptor of a process just open the file the file descriptor "points" to.
Of course you need the rights/permissions/privileges to do so. Which you have if you own both processes or if you are the root user.
EDIT: If not in bash you can also use /proc/self/...
instead of /proc/$$/...
to get info about the current process.