view output of already running processes in linux view output of already running processes in linux linux linux

view output of already running processes in linux


There is already an program that uses ptrace(2) in linux to do this, retty:

http://pasky.or.cz/dev/retty/

It works if your running program is already attached to a tty, I do not know if it will work if you run your program in background.

At least it may give some good hints. :)

You can probably retreive the exit code from the program using ptrace(2), otherwise just attach to the process using gdb -p <pid>, and it will be printed when the program dies.

You can also manipulate file descriptors using gdb:

(gdb) p close(1)$1 = 0(gdb) p creat("/tmp/stdout", 0600)$2 = 1

http://etbe.coker.com.au/2008/02/27/redirecting-output-from-a-running-process/


You could try to hook into the /proc/[pid]/fd/[012] triple, but likely that won't work.

Next idea that pops to my mind is strace -p [pid], but you'll get "prittified" output. The possible solution is to strace yourself by writing a tiny program using ptrace(2) to hook into write(2) and writing the data somewhere. It will work but is not done in just a few seconds, especially if you're not used to C programming.

Unfortunately I can't think of a program that does precisely what you want, which is why I give you a hint of how to write it yourself. Good luck!