Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JSch channel.disconnect阻止打印日志_Java_Ssh_Jsch - Fatal编程技术网

Java JSch channel.disconnect阻止打印日志

Java JSch channel.disconnect阻止打印日志,java,ssh,jsch,Java,Ssh,Jsch,我正在使用JSch在远程机器上运行shell脚本,并使用JSch通道在日志文件中打印命令日志。问题是,当脚本结束时,我执行channel.disconnect,断开连接后不久,System.out停止打印到日志文件中。代码如下: private int runShellScript(HashMap bundleDetails) { int exitStatus = -1; Channel channel = null; Session sessio

我正在使用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;
    }
在此断开之前退出状态:0

如果您在我上面粘贴的方法中看到,打印的sysout实际上就是“channel.disconnect”上方的一个。下面的那个没有打印出来!所有功能正常,总体输出符合我的预期

System.out.println(“在此断开连接之前退出状态: “+退出状态)
channel.disconnect()
System.out.println(“断开连接 此处退出状态:“+退出状态”

所有功能都正常,总体输出符合我的预期。唯一的问题是日志冻结。我哪里做错了

编辑

而且,我无法看到我最后一个街区的Syout

很可能与这条线路有关:

((ChannelExec)channel).setErrStream(System.err);
通过这样做,您已经将System.err流绑定到了通道流。根据文档,默认情况下,当通道断开时,流关闭。你没有说你在哪个平台上运行,但我认为大多数平台都以某种方式连接System.err和System.out,所以Jsch很可能在断开连接时关闭System.out。您可以尝试这样做以防止JSch关闭流:

((ChannelExec)channel).setErrStream(System.err, true);

尽管如此,我认为这样做还是有点冒险的。我认为更安全的设计是创建一个直接写入日志文件的流,而不是通过System.err。

这是因为

((ChannelExec)channel).setErrStream(System.err);
当您断开通道连接时,连接的流也会断开连接

因此,请在断开通道之前写下以下语句:

((ChannelExec)channel).setErrStream(null);

哇!工作完美无瑕。帕夫,我错过了。非常感谢卡拉奇的快速反应。我一定会按照你的建议去做!!
((ChannelExec)channel).setErrStream(null);