简体   繁体   English

通过Putty连接到远程主机(OS Solaris)并丢失连接时,如何捕获Java应用程序输出(System.out.println())?

[英]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? 如果通过远程会话丢失并再次连接时通过Putty连接到远程主机(OS Solaris)时如何捕获Java应用程序输出( System.out.println() )? Java application on remote host was started like this: 远程主机上的Java应用程序是这样启动的:

java -jar /SomeApplication.jar & 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: 但是,如果退出远程会话并再次连接-尽管仍在运行相同的Java应用程序,则看不到更多输出-我可以通过发出以下命令来看到它:

ps -ef | ps -ef | grep java grep Java

Output is: 输出为:

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

The application ID is 5221 and it is running, but it's output vanishes somewhere.. 该应用程序ID为5221,并且正在运行,但是其输出在某处消失。

A quick and neat suggestion is running the application in a screen session (check the Solaris package). 快速而整洁的建议是在screen会话中运行该应用程序(检查Solaris软件包)。

First, you launch screen: 首先,启动屏幕:

$ screen

Then you launch the application inside screen , like usual: 然后像往常一样screen启动应用程序:

$ 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. 使用CTRL + a d (首先使用CTRL + a在一起,然后使用d )与会话分离 ,以便离开screen会话(在其中运行Java应用程序),返回Solaris Shell并执行其他操作-包括优雅地登出。

  • Use CTRL + a ] to enter "copy mode", so that you can scroll up and down and q to leave copy mode. 使用CTRL + a ]进入“复印模式”,以便您可以上下滚动,并使用q退出复印模式。

There are many other useful features, just read a quick intro :) 还有许多其他有用的功能,只需阅读快速介绍:)

Log files play better in these scenarios.And sysouts are expensive operations too. 在这些情况下,日志文件的播放效果更好。sysouts也是昂贵的操作。 Or when you start the java program, redirect error/outputs to a temp file. 或者,当您启动Java程序时,将错误/输出重定向到临时文件。 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. >将system.out.println重定向到文件,2>&1将system.err.println发送到同一文件(您也可以放2> error_file.log以获取单独的System.err.println()输出。

Long term though you should use a logging framework through like log4j or the java logging framework. 从长远来看,尽管您应该通过log4j或java日志记录框架来使用日志记录框架。

If you want to watch the output then as it grows, you can do a tail -f whatever_you_want_output.log 如果您想观察输出,然后随着输出的增加,可以执行tail -f whatever_you_want_output.log

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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