我正在使用JSch在远程计算机上运行shell脚本,并使用JSch通道在日志文件中打印命令日志。问题是,当脚本结束时,我执行一个channel.disconnect,在断开连接后不久,System.out停止打印到日志文件中。以下是代码:
private int runShellScript(HashMap bundleDetails) {
int exitStatus = -1;
Channel channel = null;
Session session = null;
try {
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
String host = (String) bundleDetails.get("host");
String userName = (String) bundleDetails.get("userName");
String password = (String) bundleDetails.get("password");
String bundleName = findFileName((String) bundleDetails.get("bundleName"));
String sourceLocation = (String) bundleDetails.get("sourceLocation");
String logFileName = findFileName((String) bundleDetails.get("logFileName"));
String targetLocation = (String)bundleDetails.get("targetLocation");
String command1 = "sh "+(String) bundleDetails.get("targetIndexerLocation") + (String) bundleDetails.get("deployScript")+" "+
targetLocation + bundleName + " " +
targetLocation + logFileName;
JSch ssh = new JSch();
session = ssh.getSession(userName, host, 22);
session.setConfig(config);
session.setPassword(password);
session.connect();
channel = session.openChannel("exec");
((ChannelExec)channel).setCommand(command1);
channel.setInputStream(null);
((ChannelExec)channel).setErrStream(System.err);
InputStream in=channel.getInputStream();
channel.connect();
byte[] tmp=new byte[1024];
while(true){
//System.out.println("inside while second");
while(in.available()>0){
int i=in.read(tmp, 0, 1024);
if(i<0)break;
System.out.print("*****NEW ONE*****$$$$$**$$########"+new String(tmp, 0, i));
}
if(channel.isClosed()){
exitStatus = channel.getExitStatus();
System.out.println("Before Disconnected Here exit-status: "+exitStatus);
channel.disconnect();
System.out.println("Disconnected Here exit-status: "+exitStatus);
break;
}
}
//logger("runShellScript", "END");
System.out.println("***** out of infinite loop");
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Copy to remote location failed.... ");
}finally{
System.out.println("finally DISCONNECT channel and session");
if(channel!=null && channel.isConnected()){
channel.disconnect();
}
if(session!=null){
session.disconnect();
}
System.out.println("finally DISCONNECTED channel and session");
}
System.out.println("Before return exit-status: "+exitStatus);
return exitStatus;
}日志文件中的行:
新ONE*****$$$$$**$$########Starting..。 在此断开连接之前,退出状态:0
如果您在我上面粘贴的方法中看到,打印的sysout实际上就在“printed el.disconnection”上面。下面的那个不是印刷的!所有的功能都是正确的,总体输出是我所期望的。
System.out.println(“在这里断开连接前退出-状态:"+exitStatus); channel.disconnect(); System.out.println(“断开此处退出状态:"+exitStatus);
所有的功能都是正确的,而总体输出正是我所期望的。唯一的问题是原木冻结。我哪里出问题了?
编辑
而且,我也无法从我的最后一个街区看到系统!
发布于 2013-11-26 22:36:32
它很可能与这一行有关:
((ChannelExec)channel).setErrStream(System.err);通过这样做,您已经将System.err流绑定到了通道流。根据文档,默认情况下,当通道断开时,流将关闭。您没有说明运行在哪个平台上,但我认为大多数平台都以某种方式连接System.err和System.out,因此Jsch很可能在System.out断开时关闭System.out。您可以尝试这样做,以防止JSch关闭流:
((ChannelExec)channel).setErrStream(System.err, true);Javadoc
尽管这样做确实有效,但我认为像这样连接到System.err还是有点冒险。我认为更安全的设计是创建一个直接写入日志文件的流,而不是通过System.err。
发布于 2014-05-13 10:44:50
这是因为
((ChannelExec)channel).setErrStream(System.err);当您断开通道时,连接的流也被断开。
因此,请在断开通道之前写出以下语句:
((ChannelExec)channel).setErrStream(null);https://stackoverflow.com/questions/20229001
复制相似问题