Skip to main content
added 2 characters in body
Source Link
user147505
user147505

You can do this in Linux. Say there's a script s:

#!/bin/bash
i=0
echo my pid: "$$"
while true; do
    echo "$i"; ((i++)); sleep 1; 
done

Run it:

$ ./s
my pid: 8815
0
1
2

And so it goes... Now you need gdb to attach to the process.

gdb -p 8815

Now you're in gdb. Here do this:

p close(1)

This has closed the STDOUT file descriptor. Now open a new one:

p creat("/tmp/8815.out", 0600)

In another terminal you can already set the reading process:

tail -f /tmp/8815.out

Nothing's coming yet, as you need to detach from gdb in the other terminal. Either Ctl+D, or:

q
Quit anyway? (y or n) 

Confirm with y and Enter. And now you should see (where the tail is running) something like this:

173
174
175

If you prefer to use an already existing file or pipe, do this in gdb:

p open("/path/to/your/output", 1)

This illustrates redirecting STDOUT. It's similar with STDERR, only it has number 2.


I used these two answers:

You can do this in Linux. Say there's a script s:

#!/bin/bash
i=0
echo my pid: "$$"
while true; do
    echo "$i"; ((i++)); sleep 1; 
done

Run it:

$ ./s
my pid: 8815
0
1
2

And so it goes... Now you need gdb to attach to the process.

gdb -p 8815

Now you're in gdb. Here do this:

p close(1)

This has closed the STDOUT file descriptor. Now open a new one:

p creat("/tmp/8815.out", 0600)

In another terminal you can already set the reading process:

tail -f /tmp/8815.out

Nothing's coming yet, as you need to detach from gdb in the other terminal. Either Ctl+D, or:

q
Quit anyway? (y or n) 

Confirm with y and Enter. And now you should see (where the tail is running) something like this:

173
174
175

If you prefer to use an already existing file or pipe, do this in gdb:

open("/path/to/your/output", 1)

This illustrates redirecting STDOUT. It's similar with STDERR, only it has number 2.


I used these two answers:

You can do this in Linux. Say there's a script s:

#!/bin/bash
i=0
echo my pid: "$$"
while true; do
    echo "$i"; ((i++)); sleep 1; 
done

Run it:

$ ./s
my pid: 8815
0
1
2

And so it goes... Now you need gdb to attach to the process.

gdb -p 8815

Now you're in gdb. Here do this:

p close(1)

This has closed the STDOUT file descriptor. Now open a new one:

p creat("/tmp/8815.out", 0600)

In another terminal you can already set the reading process:

tail -f /tmp/8815.out

Nothing's coming yet, as you need to detach from gdb in the other terminal. Either Ctl+D, or:

q
Quit anyway? (y or n) 

Confirm with y and Enter. And now you should see (where the tail is running) something like this:

173
174
175

If you prefer to use an already existing file or pipe, do this in gdb:

p open("/path/to/your/output", 1)

This illustrates redirecting STDOUT. It's similar with STDERR, only it has number 2.


I used these two answers:

Source Link
user147505
user147505

You can do this in Linux. Say there's a script s:

#!/bin/bash
i=0
echo my pid: "$$"
while true; do
    echo "$i"; ((i++)); sleep 1; 
done

Run it:

$ ./s
my pid: 8815
0
1
2

And so it goes... Now you need gdb to attach to the process.

gdb -p 8815

Now you're in gdb. Here do this:

p close(1)

This has closed the STDOUT file descriptor. Now open a new one:

p creat("/tmp/8815.out", 0600)

In another terminal you can already set the reading process:

tail -f /tmp/8815.out

Nothing's coming yet, as you need to detach from gdb in the other terminal. Either Ctl+D, or:

q
Quit anyway? (y or n) 

Confirm with y and Enter. And now you should see (where the tail is running) something like this:

173
174
175

If you prefer to use an already existing file or pipe, do this in gdb:

open("/path/to/your/output", 1)

This illustrates redirecting STDOUT. It's similar with STDERR, only it has number 2.


I used these two answers: