29

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.

Lesmana
  • 27,439

4 Answers4

11

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).

  • 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
6

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.

3

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:

https://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.

1

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.