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.

If I close the virtual terminal, where some process was started, does the output just go straight to /dev/null, or can it pollute memory somehow? Can I anyhow grab the output to continue reading it at any point after that?

[EDIT]: So, is the moment of disowning a process effectively an end of my power to control its output?

I also noticed, that if I disown a stopped process, at first everything seems normal: it is neither terminated nor shown in jobs. But if I log out (and I don't mean close the terminal, just exit from su, for instance), the process is terminated. All the same, a background-running disowned process can stay running.

share|improve this question
2  
For the “can I anyhow grab the output”: not without dirty tricks. But see How can I disown it a running process and associate it to a new screen shell? and other cited questions for dirty tricks (all based on attaching a debugger to the program and somehow making it open a different output file). –  Gilles Jul 31 '11 at 21:05
    
Thanks for the link to that question. It gave me the best answer so far! Especially the clever retty program. –  rozcietrzewiacz Aug 2 '11 at 12:47
1  
See also this answer on a related question. –  Stéphane Gimenez Aug 2 '11 at 23:10
add comment

4 Answers

The fact that a process is "disowned" has only a meaning for the interactive shell that created this process. It means that the shell doesn't include (anymore) the process in its jobs table, and that SIGHUP will not be sent to this process when the shell exits. It is not really related with your questions.

About what happens to the outputs that are sent to a deleted virtual terminal: I made some tests myself, and I noticed that /dev/pts/x devices are not accessible, and won't be allocated again until all filedescriptors that point to them have been closed. So, I can't see a reason why writes to a deleted terminal would be stored. I guess this is not even defined by POSIX.

About grabbing the output of some process that writes to a terminal, I don't think it is possible, even when the terminal is still alive¹. All you can do is grabbing the direct input to the terminal (i.e. keystrokes, or simulated keystrokes by the master part of a pty). If processes would read on stdin what is written to their terminals, that would lead to a self io loop for most process.

About the last remark on process termination, I don't really know what is happening, but I would suspect rather strange behaviors with signals (SIGTTOU, SIGTTIN, SIGHUP, or others) related to foreground/background state of process groups, when the session leader exits (e.g. su, in the case you mentioned).

Answer to the Edit: No, with respect to output, nothing changes when a process is disowned: it is still attached to its controlling terminal (unless it detached itself already like daemons do). You can see that using ps. However, you will not be able to use fg/bg/jobs commands provided by the shell anymore for this process. That means it might be difficult to feed it with input from the terminal (requires to be in the foreground process group).


1. unless the process is willing to, or hijacked with some debugging tools (see comments above).

share|improve this answer
1  
Thanks for clarifying this a bit. Actually the fact of disowning a process is still related to my question: After disowning a process, I seem to loose the ability to control its output, right? (Even if the terminal has nit been closed.) I'll edit the question to include that case. –  rozcietrzewiacz Jul 31 '11 at 16:17
add comment

To answer the most interesting part of your question: to change the output of a live running program, you have to edit its file descriptors. That is quite easy to do with gdb. It's a hack, but works.

See:

http://stackoverflow.com/questions/593724/redirect-stderr-stdout-of-a-process-after-its-been-started-using-command-line

A helper script is available at http://users.linpro.no/ingvar/fdswap.sh.txt.

share|improve this answer
add comment

Just to address this specific question:

If I close the virtual terminal, where some process was started, does the output just go straight to /dev/null, or can it pollute memory somehow?

The terminal and the program(s) connected to it communicate via a tty device by reading and writing it like a file. Specifically, a virtual terminal creates a "pseudo-tty" ("pty" for short) and then spawns a shell (or other) process and connects the stdin/out/err of that process to the pty. (The details vary by operating system.)

When you close the virtual terminal, the virtual terminal closes its end of the connection (the pty "master"). After that, if the program at the other end of the connection writes to the tty, an error is returned and the data doesn't go anywhere. Similarly, if it reads from the tty it will get back an EOF (end of file) indicator.

share|improve this answer
    
Thanks - nice and clear explanation from a bit more programming point of view. –  rozcietrzewiacz Sep 4 '11 at 12:56
add comment

Thanks to comment by Gilles, pointing me to this question, I learned about a program called retty.

It seems to use some dirty hack to reattach to a (pseudo-)tty effectively allowing to continue reading the output of a process - no matter whether it has been disowned or not. So this seems to answer most of the first part of my question. The second was answered by Stéphane.

share|improve this answer
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.