Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OH3.0.0: ExecUtil.executeCommandLine() is not returning result if command return code !=0 #2033

Closed
olialb opened this issue Dec 30, 2020 · 10 comments · Fixed by #2104
Closed

Comments

@olialb
Copy link

olialb commented Dec 30, 2020

Just migrate from OH2 to OH3 and I am in general very happy with the new version...

But I am porting my executeCommandLine commands in my rules from OH2 to OH3 and I observerd that some commands are not excecuted because the return value of executeCommandLine is null or "". But it seams they are actually exceuted, only the output is not catched in the result variable.
After playing arround with python2 and python3 scripts I come to the following conclusion:

If the command line returns something !=0 the output is not catched and empty. Also there is no error in the log.

This can be reproduced with the following rule content

var String ScriptPath = "/etc/openhab/scripts/"
var result = executeCommandLine(Duration.ofSeconds(50),"python", ScriptPath+"test.py")

And and the script test.py:

import sys
print("Hallo World")
sys.exit(1)

the result is null or "".

If the script is modified like this:

import sys
print("Hallo World")
sys.exit(0)

result is "Hello World" as expected.
I would say this is a bug or is it a feature? For sure it is not nice that there is not any output in the log if the return value is != 0 and the result variable is empty. Also its diffrent undocumented behaviour to OH2.

See also here: https://community.openhab.org/t/oh3-openhabianpi-executecommandline-returns-always-with-empty-result-for-python2-commands/112056

@olialb olialb changed the title OH3: ExecUtil.executeCommandLine() is not returning result if command return code !=0 OH3.0.0: ExecUtil.executeCommandLine() is not returning result if command return code !=0 Dec 30, 2020
@Stefan300381
Copy link

Hi,

I face the same issue when I run some ping and ssh commands.
Here a quick example:

        //this ip address does not exist in my LAN
        var result = executeCommandLine(Duration.ofSeconds(5),"/usr/bin/ping", "-c", "1", "192.168.2.254")
        logInfo("ping", result) 

        //this ip address DOES exist in my LAN
        result = executeCommandLine(Duration.ofSeconds(5),"/usr/bin/ping", "-c", "1", "192.168.2.10")
        logInfo("ping", result) 

As you can see, first result is null:

2021-01-01 22:57:24.717 [INFO ] [org.openhab.core.model.script.ping  ] - null

2021-01-01 22:57:24.778 [INFO ] [org.openhab.core.model.script.ping  ] - PING 192.168.2.10 (192.168.2.10) 56(84) bytes of data.
64 bytes from 192.168.2.10: icmp_seq=1 ttl=64 time=0.535 ms
--- 192.168.2.10 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.535/0.535/0.535/0.000 ms

Stefan

@Boby71
Copy link

Boby71 commented Jan 3, 2021

Still the same in openHAB 3.1.0. Build #2112

@Boby71
Copy link

Boby71 commented Jan 10, 2021

@kaikreuzer, I also noticed this problem of RC=null when a (DSL) rule ends up with an RC<>0. This makes it extremely hard to find the problem when rules aren't working in OH3. I'm just mentioning you here as I'm not sure if the dev team is aware of this issue. It's still the same in 3.1.0-SNAPSHOT - Build #2127. Thank you!

@kaikreuzer
Copy link
Member

kaikreuzer commented Jan 11, 2021

This looks like a regression introduced by #1700 to me as this PR explicitly returns null in case of an error RC.

@cpmeister Can I assume that this was unintentional?

@kaikreuzer
Copy link
Member

I've created #2104 as a fix.

@andrewfg
Copy link
Contributor

I am seeing a different but similar error..

  • If you run executeCommandLine() on a "fast" command such as ls then it does return the full console output, but
  • If you run it on a "slow" command (such as wget or curl) it does NOT return the full console output; it returns an empty string

See this..
https://community.openhab.org/t/oh3-migrated-rule-executecommandline-command-fails/114027

Perhaps related to this..
#1677

@andrewfg
Copy link
Contributor

@kaikreuzer @wborn @cpmeister

Here is my test case rule, that illustrates it working for ls command but not for wget command..

rule "ZZZ Download Weather Icon"
when
    Item Local_Weather_Current_Icon_Id received update
then
  var response = executeCommandLine(Duration.ofSeconds(5), "ls");
  logWarn("Rules", "ls response: " + response)
  //
  val sourceIcon  = "https://darksky.net/images/weather-icons/rain.png"
  val cacheFolder = "/etc/openhab/icons/classic/weather-icons/"
  response = executeCommandLine(Duration.ofSeconds(5), "wget", "-N", "-P", cacheFolder, sourceIcon)
  logWarn("Rules", "wget response: " + response)
end

Log outputs:- ls returns the full console output, but wget does not; (even though wget does successfully download the file)

2021-01-13 17:14:33.059 [WARN ] [org.openhab.core.model.script.Rules ] - ls response: cache
config
etc
jsondb
persistence
secrets
tmp
zwave

2021-01-13 17:14:33.622 [WARN ] [org.openhab.core.model.script.Rules ] - wget response: 

@andrewfg
Copy link
Contributor

On the Community Forum Wolfgang_S said the following (I quote)..

As far as I can see wget does not write something to STDOUT. I think that is the reason why no result is being returned. The output that is written by wget is written to STDERR. In case you change the wget command and add an arguments like: -O - then the downloaded icon will be written to STDOUT ( which does not make sense because it is binary data ) but you will see that this returned value can e.g. written to loginfo.

Therefore it seems to me that the correct solution is..

  • Instead of switching on "return code !=0" it should rather be as follows
  • If STDOUT returns something then executeCommandLine() should return this, but if not
  • If STDERR returns something then executeCommandLine() should return this

@andrewfg
Copy link
Contributor

New PR opened #2114

@Boby71
Copy link

Boby71 commented Jan 15, 2021

Thanks, I can confirm the bug has been fixed (tested on 3.1.0-SNAPSHOT - Build #2143)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants