Search code examples
linuxprocessps

view output of already running processes in linux


I have a process that is running in the background (sh script) and I wonder if it is possible to view the output of this process without having to interrupt it.

The process ran by some application otherwise I would have attached it to a screen for later viewing. It might take an hour to finish and i want to make sure it's running normally with no errors.


Solution

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