0

I am executing a command on the remote machine using JSch as below:

public String executeCommandNWaitForCompletion(String hostIp, String userName, String password, String command) throws Exception {
    Session session = null;
    ChannelExec channel = null;
    InputStream in = null;
    String response = "";
    try {
        session = getSession(hostIp, userName, password);
        channel = (ChannelExec) session.openChannel("exec");
        channel.setCommand(command);
        channel.setInputStream(null);
        channel.setErrStream(System.err);
        in = channel.getInputStream();
        channel.connect();
        byte[] tmp = new byte[1024];
        while (true) {
            while (in.available() > 0) {
                int i = in.read(tmp, 0, 1024);
                if (i < 0) break;
                response += new String(tmp, 0, i);
            }
            if (channel.isClosed()) {
                if (in.available() > 0) continue;
                log.debug("Exit-status: " + channel.getExitStatus());
                break;
            }
        }

    } finally {
        if (in != null) in.close();
        closeResources(session, channel);
    }
    return response;
}

In tomcat docker container this method is called from a spring mvc controller. Entire application works well but when this method is called, docker container logs are getting hanged and nothing gets displayed after that. But application runs as usual only logs gets hanged.

This method was running fine when we're running our app on Tomcat server directly but does not work in a docker container.

How to resolve this issue?

user1188867
  • 3,726
  • 5
  • 43
  • 69
  • Any command hangs the logs? Even something as `pwd`? – Martin Prikryl Apr 04 '19 at 10:31
  • Yes @MartinPrikryl even running pwd command hangs the logs but in the background it runs perfectly. – user1188867 Apr 04 '19 at 10:56
  • And is this **minimal** code that hangs the logs? What if you remove the inner reading loop? What if you remove whole outer waiting loop? What if you remove whole channel stuff? – Martin Prikryl Apr 04 '19 at 11:29
  • Yes this minimal code is hanging the logs. I can remove and check the reading loops but the control has to wait until the execution of the command and the issue is the same exact code works in standalone tomcat. – user1188867 Apr 05 '19 at 03:49
  • 1
    That's really ambiguous answer. So can you or cannot you remove the loop to still hang the logs? (I understand that the code will be useless then, but we need to find out what code hangs the logs now) – Martin Prikryl Apr 05 '19 at 04:19
  • See also [How to read JSch command output?](https://stackoverflow.com/q/6902386/850848) – Which shows how to *correctly* read both standard and error output simultaneously, to allow command to complete and to collect all output including the errors. – Martin Prikryl Nov 11 '20 at 16:24

1 Answers1

0

I have removed the below 2 lines in the code to resolve the issue:

channel.setInputStream(null);
channel.setErrStream(System.err);
user1188867
  • 3,726
  • 5
  • 43
  • 69