Exec Binding 2.0 not working (exception while reading the stderr)

Hi,

I’m trying the new Exec binding but it won’t work for me. I’m using Build #614 (Debian package openhab2-online 2.0.0~20161126032927).

system.items

String System_OpenFiles "Anzahl offener Dateien" { channel="exec:command:open_files:output" }

system.things

Thing exec:command:open_files [ command="sudo /usr/bin/lsof -u openhab |wc -l", interval=5, timeout=3 ]

Log output

2016-11-27 13:50:57.651 [ERROR] [hab.binding.exec.handler.ExecHandler] - An exception occured while reading the stderrwhen executing 'sudo /usr/bin/lsof -u openhab |wc -l' : 'Stream closed'

I think this is en error in the binding. Looking at the source code at https://github.com/openhab/openhab2-addons/blob/8d5af21df40440f018a17bfd392ce4b3376a0df5/addons/binding/org.openhab.binding.exec/src/main/java/org/openhab/binding/exec/handler/ExecHandler.java, I think at line 143 it should read proc.getOutputStream() instead of proc.getErrorStream().

try (InputStreamReader isr = new InputStreamReader(proc.getErrorStream());
                                                     // ^^^^^^^^^^^^^^
        BufferedReader br = new BufferedReader(isr);) {
    String line = null;
    while ((line = br.readLine()) != null) {
        outputBuilder.append(line).append("\n");
        logger.debug("Exec [{}]: '{}'", "OUTPUT", line);
    }
    isr.close();
} catch (IOException e) {
    logger.error("An exception occured while reading the stdout when executing '{}' : '{}'",
            new Object[] { commandLine.toString(), e.getMessage() });
}

Please let me know if my thinking is right.

Thanks,
Aaron

I have opened an issue (https://github.com/openhab/openhab2-addons/issues/1491) for this. The only thing I am now wondering if the streams got combined, I don’t remember and need to check. It does work for me on Mac OS X Sierra.

Thanks for fixing this so quickly :slight_smile: I just saw the pull request has already been merged. Great work!

I just installed the latest version of the binding (2.0.0.201611281907) and can confirm that the error is gone :slight_smile:

However, the above example doesn’t work because it seems to escape the pipe character. The output I get is the following:

lsof: status error on |wc: No such file or directory
lsof: status error on -l: No such file or directory

I can easily fix this by putting the command in a script. However, I’d be curious if this is the intended behavior.