I've run a command and redirected it's output via > /dev/null
Now that it's been running significantly longer than I expected I want to see what it's doing.
Is there a way to re-redirect the output, such that all the new contents would be printed to stdout
? I realize that all the previous contents are gone.
Best Answer
You can do it using strace.
Using
strace
you can spy what is being written to file-descriptor 1, which is the stdout file descriptor. Here is an example:You may want to improve the filter, but that would be another question. We have the output, but now need to tidy it.
:WARNING: This solution has some limitations, see comments below. It will not always work, your mileage may vary.
Test:
Put this program (below) in file
hello
, andchmod +x hello
This one in
hello1
andchmod +x hello1
This one in
hello2
andchmod +x hello2
then run with
./hello2 >/dev/null
, then find pid of process hello and typepid_of_process_you_want_to_see_stdout_of=xyz
where xyz is the pid of hello, then run line at top.How it works. When hello is run, bash forks, redirects fd 1 to
/dev/null
, then execs hello. Hello sends output to fd1 using system callwrite(1, …
. Kernel receives system callwrite(1, …
, sees that fd 1 is connected to/dev/null
and …We then run strace (system-call trace) on hello, and see that it is calling
write(1, "hello\nworld\n")
The rest if the line above is just selecting the appropriate line of the trace.