Linux – How to see the current running Linux processes output^

linuxoutputprocessterminalverbose

I have several Linux (Ubuntu 10.04) processes running on my computer. If I start one of them, I can see the verbose of it on my terminal. I have an other process that start a dozen of those processes so that they run in background. However, I would like to watch the output of one of these processes to see if the output is still ok and there are no error message. I know I could send everything into a log message, however, this would just use too much disk space. So, is there a way to "catch" the output of a running process in Linux using it's process Id?

Best Solution

You can attach gdb to your running process and redirect stdout/err or both to some log file whenever you feel like looking at the output (make sure the files exist before redirecting):

gdb attach PID
  (gdb) p dup2(open("/tmp/mylogfile-stdout", 0), 1)
  (gdb) p dup2(open("/tmp/mylogfile-stderr", 0), 2)
  (gdb) detach
  (gdb) quit

When you want them to go back to being silent just do:

gdb attach PID
  (gdb) p dup2(open("/dev/null", 0), 1)
  (gdb) p dup2(open("/dev/null", 0), 2)
  (gdb) detach
  (gdb) quit

The 'detach' part is important, otherwise you will kill the process you're attached to when gdb exits. For more details see this question.