Henridv
Henridv

Reputation: 1094

Intercept output from a running Java program

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?

Upvotes: 2

Views: 627

Answers (2)

Uku Loskit
Uku Loskit

Reputation: 42040

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.

Upvotes: 6

jmkeyes
jmkeyes

Reputation: 3771

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.

Upvotes: 0

Related Questions