0

Is there any way to redirect stdout (1) from that "pipe" (I don't know exacly how I suppose interpret this, I will be glad if someone could explain how to treat this, or give me some read on this) to some other output, eg. file or terminal?

-bash-4.2$ ls -l /proc/11/fd
total 0
lrwx------ 1 us sudo 64 Sep 24 11:26 0 -> /dev/null
l-wx------ 1 us sudo 64 Sep 24 11:26 1 -> pipe:[20619]
l-wx------ 1 us sudo 64 Sep 24 11:26 2 -> pipe:[20620]
lrwx------ 1 us sudo 64 Sep 24 11:26 3 -> socket:[30376]
lr-x------ 1 us sudo 64 Sep 24 11:26 4 -> /dev/null
l-wx------ 1 us sudo 64 Sep 24 11:26 5 -> pipe:[30639]
lrwx------ 1 us sudo 64 Sep 24 11:26 6 -> socket:[27522]
0

1 Answer 1

1

Not in a clean or portable way. You have to attach with a debugger like gdb, open some the destination file and dup it into fd 1. As with

gdb -p <PID> -batch -ex 'call dup2(open("<PATH>", 2), 1)'

That pipe:[digits] is an "anonymous" pipe, as created by cmd | cmd shell construct.

However on Linux it's not really anonymous, since you can open it via /proc/<PID>/fd/<NUM>. So you have another option (which is guaranteed to wreak even more havoc than using gdb): open the other side of the pipe, kill whatever program is reading from it, and cat it somewhere else. Stupid example:

% while sleep 1; do TZ=Zulu date; done | wc -c &
[1] 26727
% ps
  PID TTY          TIME CMD
20330 pts/1    00:00:00 bash
26726 pts/1    00:00:00 bash     # this the while ... done process
26727 pts/1    00:00:00 wc
26745 pts/1    00:00:00 sleep
26746 pts/1    00:00:00 ps
% ls -l /proc/26726/fd/1
... /proc/26726/fd/1 -> 'pipe:[1294932]'
% exec 7</proc/26726/fd/1        # open the other side of the pipe
% kill 26727                     # kill wc -c
% cat <&7
Fri 24 Sep 2021 01:25:52 PM UTC
Fri 24 Sep 2021 01:25:53 PM UTC
Fri 24 Sep 2021 01:25:54 PM UTC
...

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.