Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I know how to redirect output and how to suppress them in bash. Now, suppose I accidentally forgot to append the output redirection part to the command (e.g. 2>&1 or > /tmp/mystdout) and my background process is already running for a while, can I still change to where stdout and stderr are being written to? I really would like not to kill and restart the application.

To be more specific as asked by Gilles in his comment, I would like to fiddle with it in these scenarios in specific:

  • wrong output file
  • forgot to redirect stderr to stdout

or a combination of both

E.g. I have Apache running and I can see the file descriptors:

/proc/8019/fd/0 -> /dev/null
/proc/8019/fd/1 -> /dev/null
/proc/8019/fd/2 -> /var/log/apache2/error.log
share|improve this question
    
Change to another terminal? Or to a file? If you're content with attaching to the current terminal or a new screen session, see How can I disown a running process and associate it to a new screen shell? –  Gilles Dec 21 '12 at 23:45
add comment

2 Answers 2

up vote 0 down vote accepted

That's not possible. Or at least not easy. You might have some luck attaching gdb to the process and fiddling with it in gdb, but that can lead to crashes just as easily as success.

share|improve this answer
    
There are tools for this nowadays. –  Gilles Dec 21 '12 at 23:46
    
Feel free to share them, I'll upvote the answer :) –  Dennis Kaarsemaker Dec 21 '12 at 23:47
    
Um, I already did. –  Gilles Dec 22 '12 at 0:03
    
Not quite. All tools linked in the other bug are about attaching to another terminal/screen session. Not about redirecting to another file. Though it's probably not hard to patch reptyr to redirect to a file instead. –  Dennis Kaarsemaker Dec 22 '12 at 0:11
    
I've looked a bit into reptyr's source and I agree most with the "not easy" part of the answer. Might take a deeper dive into it some time later. –  gertvdijk Jan 4 '13 at 10:24
add comment

You should use reptyr to attach another process to your new current terminal. For example: install and use GNU's screen utility, and in it launch the reptyr command to "fetch" the other process's redirections (and controlling terminal) and attach them to the new current terminal (the screen process, if you launched reptyr from screen). The advantage of doing this from a screen session is that, once under screen, it will then be easy to disconnect and reconnect to it (for example, closing your local session, going home, and reopening it).

share|improve this answer
    
Thanks for your answer, but it doesn't help me with my case as I am not asking about terminal detach/retach. Please re-read my question and the example in specific. –  gertvdijk Dec 26 '12 at 15:36
    
ah, in that case : read the reptyr code, find out how to modify it to only redirect stdout or stderr. Then run it from another shell that redirects that newly-attached stream to where you want it to go? (or, better, using the reptyr source, do the redirection directly) –  Olivier Dulac Dec 26 '12 at 15:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.