【问题标题】:JSch interrupt command executingJSch中断命令执行
【发布时间】:2020-01-05 18:49:27
【问题描述】:

我有两个用于运行和停止执行 JSch 命令的按钮的 Swing 应用程序。 所以我有下一个方法,当我们点击开始按钮时会调用它:

public String executeSSHCommand(String pattern, String listFiles) {             
    searchRez = new StringBuilder();        
    BufferedReader br = null;
    String line, cmd = null;
    // adding ' symbol to pattern
    pattern = "'"+pattern+"'";       
    //
    try {
        Channel channel = session.openChannel("exec");            
        cmd = "zgrep " + pattern + " " + listFiles;
        log.debug("Executing next ssh command: " + cmd);                        
        ((ChannelExec)channel).setCommand(cmd);                        
        channel.setInputStream(null);
        ((ChannelExec)channel).setErrStream(System.err);             
        InputStream in=channel.getInputStream();
        channel.connect();
        // read server output
        br = new BufferedReader(new InputStreamReader(in));
        while(true && !isCancel){               
          while ((line = br.readLine()) != null && !isCancel) {                        
            searchRez.append(line+"\n");                        
          }                         
          if(channel.isClosed()){
              log.debug("Exit status for execute ssh command is " + channel.getExitStatus());                
            break;
          }
          try{Thread.sleep(1000);}catch(Exception ee){log.error(ee);}
        }

        if (isCancel) {
            log.debug("Search was canceled by user... ");                

            channel.setInputStream(null);                
            if (in != null) {
                in.close();
                in = null;
            }
            if (br != null) {
                br.close();
                br = null;
            }
            try{Thread.sleep(1000);}catch(Exception ee){log.error(ee);}
        }
        //
        channel.disconnect();   
        session.disconnect();         
        log.debug("Search pattern in log file is complete.");
    } catch (Exception ex) {
        log.error(ex);
    }               
    log.debug("Rezult string has next value: " + searchRez.toString());
    return searchRez.toString();
}

正如您在上面看到的,我们有 isCancel 变量,它是布尔型 volatile 变量,我们在单击停止按钮时将其设置为 false。所以我的问题是当用户单击停止按钮并且会话通道断开连接时,zgrep 表达式继续在服务器上工作。我认为 channel.disconnect() 应该停止我在服务器上运行的所有命令,有人可以建议如何解决这个问题。谢谢。

【问题讨论】:

    标签: java swing jsch


    【解决方案1】:

    @dic19 给了我一个想法,在 断开通道和会话之前将Ctrl+C 命令发送到服务器,以中断服务器端的zgrep 命令执行。

    我在 JSch 邮件列表中找到了this post,它帮助我解决了我的问题。

    所以要发送Ctrl+C 命令,您应该为通道对象((ChannelExec)channel).setPty(true); 启用putty 模式并将3 值写入通道OutputStream:

            ((ChannelExec)channel).setPty(true);
            ((ChannelExec)channel).setCommand(cmd);                        
            channel.setInputStream(null);
            ((ChannelExec)channel).setErrStream(System.err);             
            InputStream in=channel.getInputStream();
            OutputStream out = channel.getOutputStream();
            channel.connect();
            ...........
            if (isCancel) {
                log.debug("Search was canceled by user... ");                
    
                channel.setInputStream(null);                
                if (in != null) {
                    in.close();
                    in = null;
                }
                if (br != null) {
                    br.close();
                    br = null;
                }
                out.write(3);
                out.flush();
                try{Thread.sleep(1000);}catch(Exception ee){log.error(ee);}
            }
    

    【讨论】:

    • +1 但是请添加解决您的问题的代码,这样万一链接被删除,您的答案仍然完整。此外,未来的访问者无需导航到外部资源即可找到答案。
    • CTRL+C 不是信号 3,即 SIGQUIT。 SIGINT 为 2,请参阅 man-page of signal
    【解决方案2】:

    除了将 pty 设置为 true,您还可以简单地使用:

    ((ChannelExec) channel).sendSignal("INT"); // may throw Exception
    

    即使您已经调用了out.close();(out 与正在运行的命令的stdin 相关),这仍然有效。

    【讨论】:

      猜你喜欢
      • 2022-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-06
      • 2011-08-15
      • 2012-08-31
      相关资源
      最近更新 更多