简体   繁体   中英

How to catch Java application output (System.out.println()) when connected to remote host (OS Solaris) via Putty and connection is lost?

How to catch Java application output ( System.out.println() ) when connected to remote host (OS Solaris) via Putty if remote session is lost and connected again? Java application on remote host was started like this:

java -jar /SomeApplication.jar &

Now the output is visible. But if I exit remote session and connect again - no more output is visible, though the same java application is still running - I can see it by issuing this command:

ps -ef | grep java

Output is:

root 5221 1 0 20:04:15 ? 1:20 java -jar SomeApplication.jar

The application ID is 5221 and it is running, but it's output vanishes somewhere..

A quick and neat suggestion is running the application in a screen session (check the Solaris package).

First, you launch screen:

$ screen

Then you launch the application inside screen , like usual:

$ java -jar /SomeApplication.jar

And you are set. You don't need the trailing & : you can detach (see below) and log out; the screen session will be kept running. On next login, just issue:

$ screen -x

and the session, with its output, will be reattached .

A quick reference:

  • Use CTRL + a d (first CTRL + a together, then d ) to detach from the session, so that you leave the screen session (with the java application running inside), go back to the Solaris shell and do other things -- including logging out graciously.

  • Use CTRL + a ] to enter "copy mode", so that you can scroll up and down and q to leave copy mode.

There are many other useful features, just read a quick intro :)

Log files play better in these scenarios.And sysouts are expensive operations too. Or when you start the java program, redirect error/outputs to a temp file. So you can view it later.

java -jar /SomeApplication.jar > wherever_you_want_output.log 2>&1 &

the > redirects system.out.println to the file, the 2>&1 sends the system.err.println to the same file (you could also put 2> error_file.log to get a separate System.err.println() output.

Long term though you should use a logging framework through like log4j or the java logging framework.

If you want to watch the output then as it grows, you can do a tail -f whatever_you_want_output.log

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM