2

Is it possible to grab / intercept the output from an already running Java program?

This is my situation: I'm running a program which takes some hours to finish. So I start it and let the computer do it's work. I can login on the computer running the program with SSH and see the it running with ps, but I would like to intercept the output which is written to the console.

Is there any way to do this?

Do I need to provide extra information?

Henridv
  • 766
  • 12
  • 23

2 Answers2

6

Start the application in a screen session, detach from the session and later you can ssh in and reattach to see the progress. Here's some information on using screen.

Another option is to redirect the standard ouput and standard error to a file, and use something like tail to view its content. example: my_program &> /tmp/output

Ok, I just realized I was a bit offtopic, because I handled the cases where you had to think ahead. Now, if tou want to get it from a running process you can use gdb.

gdb -p process_id
(gdb) p close(1)
$1 = 0
(gdb) p creat(“/tmp/output″, 0600)
$2 = 1
(gdb) q

This will put the output to /tmp/output and again, you can use tail to see it interactively.

Uku Loskit
  • 40,868
  • 9
  • 92
  • 93
  • 1
    +1 for mentioning redirecting to file and using `tail` to view. – Perception Jan 07 '12 at 23:36
  • Thanks, this works! Is it also possible to 'detach' the new listener? I also want to note you have to do this as **root** user. – Henridv Jan 08 '12 at 09:15
  • Is it possible to explain a little what these gdb commands do? I already found this article, but it doesn't explain each command: [link](http://blog.tridgell.net/?p=4) – Henridv Jan 08 '12 at 09:31
  • To me, tail a log file is the best solution, using tail -f , however, note that in Linux you could attach a running program to & from user front & background. Just remember to get the process id. As Uku said, Screen & Tail would be great. – Araejay Jan 08 '12 at 15:48
0

Yes, you should capture it's standard input, output, and error streams. Check out $ man popen or this SO question.

EDIT: For an already running process, see if you can attach a basic debugger to the process and duplicate the input and output streams as per above.

Community
  • 1
  • 1
jmkeyes
  • 3,751
  • 17
  • 20