channel.getExitStatus() is sometimes -1

Help
Anonymous
2012-02-12
2013-05-28

  • Anonymous
    2012-02-12

    channel.getExitStatus() is sometimes -1 even when channel.isClosed().

    This is how we execute our commands:

        int rc = 0;
        channel = session.openChannel("exec");
        ((ChannelExec) channel).setCommand(cmd);
            channel.setInputStream(null);
            ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
            ((ChannelExec) channel).setErrStream(errorStream);
            InputStream in = channel.getInputStream();
            channel.connect();
            byte[] buffer = new byte[1024];
            while (!channel.isClosed()) {
                appendStreamToBuilder(outputBuilder, in, buffer);
            }
            appendStreamToBuilder(outputBuilder, in, buffer);
            // Appending the error stream
            outputBuilder.append(errorStream.toString());
            Integer exitCode = channel.getExitStatus();
            if (exitCode != null) {
                rc = exitCode;
            }
    
    private static void appendStreamToBuilder(StringBuilder outputBuilder, InputStream in, byte[] buffer) throws IOException {
        int n;
            while (-1 != (n = in.read(buffer))) {
            outputBuilder.append(new String(buffer, 0, n));
        }
    }
    

    rc is sometimes minus one (-1).
    The last time it happened the cmd was "'rm -rf /opt/backups".

    We are using jsch-0.1.44.jar, with Java 1.6.0_25