0

I want to read from stdout of long-running command. There is a program, which sends me some output and I have to send some response.

The problem is that nothing is arriving to inputstream.

What am I doing, is exec'ing command, take InputStream from command and trying to read from there.

SSHClient client = new SSHClient();
client.addHostKeyVerifier(new IgnoreHostKeyVerifier());
client.connect(getHostname(), getSshPort());
client.authPassword(getUsername(), getPassword());

Session sess = client.startSession();
Command cmd = sess.exec("/root/av_run/alarms");

InputStream is = cmd.getInputStream();
os = cmd.getOutputStream();
int piece;

while (true) {
    if (is.available() > 0) {
        piece = is.read();
    } else {
        Thread.sleep(500);
        continue;
    }
}

Here, is.available() is always zero. If I read without that condition, reading is blocked forever.

How do I read stdout of a running program?

bgnfu7re
  • 144
  • 6

1 Answers1

0

Appeared that code is fine. The problem was with the binary that I run. For some reason, it put its output to some esoteric stream. That is really strange, because in the console, when I run that command, I could see its output, but I couldn't catch it on any stream. Piping it to stdout didn't help either.

Anyway, problem solved, binary was fixed.

bgnfu7re
  • 144
  • 6