Ubuntu OH2 unable to executecommandline(curl....) via .rules

Hi folks,
After getting no where with zone2 with the yamaha binding I decided to try my luck with the execbinding, however im still running into an issue where the rule triggers and says my command is executed, but nothing happens. If I copy and paste the command form the log file it works, so i would assume the formatting is correct. I’ve read through the several related topics out there but couldnt seem to come to a solution and was concerned not all applied to my oh2 installation.

Output from Log after using my virtual switch

17:47:14.959 [INFO ] [marthome.event.ItemStateChangedEvent] - vAV_Power_Zone2 changed from OFF to ON
17:47:14.960 [INFO ] [lipse.smarthome.io.net.exec.ExecUtil] - executed commandLine '/usr/bin/curl -X POST -H "Content-Type: text/xml" -H "Accept: text/xml" -H "Cache-Control: no-cache" -d '<YAMAHA_AV cmd="PUT"><Zone_2><Power_Control><Power>On</Power></Power_Control></Zone_2></YAMAHA_AV>' ""'

Rule file

rule "Zone2"
        Item vAV_Power_Zone2 changed
        executeCommandLine("/usr/bin/curl -X POST -H \"Content-Type: text/xml\" -H \"Accept: text/xml\" -H \"Cache-Control: no-cache\" -d '<YAMAH$

Ive tried adding in @@ between the spaces, ive tried executing a script using callscript from within the rule, nothing seems to work. However if I copy the command from the logging output and run it in my usershell as myself it runs correctly. Not sure if that means permission issue, but was hoping someone could help me nail down the issue.

99% of the time the problem is one of permissions. OH is running as the openhab user. This user has limited permissions.

In any case, change your Rule to the following so you can see what the command is returning:

val results = executeCommandLine("/usr/bin/curl -X POST -H \"Content-Type: text/xml\" -H \"Accept: text/xml\" -H \"Cache-Control: no-cache\" -d '<YAMAH ...", 5000)
logInfo("test", results)

NOTE: The line pasted in above in your code is incomplete but based on the log I assume that is just a copy and paste error.

Hi Rich,

Where should I be looking to see the outputs of this logInfo? in the Karaf console with log:tail I see the following… the only new output is the script.test - line.

20:36:27.526 [INFO ] [marthome.event.ItemStateChangedEvent] - vAV_Power_Zone2 changed from OFF to ON
20:36:27.530 [INFO ] [lipse.smarthome.io.net.exec.ExecUtil] - executed commandLine '/usr/bin/curl -X POST -H "Content-Type: text/xml" -H "Accept: text/xml" -H "Cache-Control: no-cache" -d '<YAMAHA_AV cmd="PUT"><Zone_2><Power_Control><Power>On</Power></Power_Control></Zone_2></YAMAHA_AV>' ""'
20:36:27.531 [INFO ] [.eclipse.smarthome.model.script.test] -

This is what my Rules file looks like now:

rule "Zone2"
        Item vAV_Power_Zone2 changed
        val results = executeCommandLine("/usr/bin/curl -X POST -H \"Content-Type: text/xml\" -H \"Accept: text/xml\" -H \"Cache-Control: no-cache\" -d '<YAMAHA_AV cmd=\"PUT\"><Zone_2><Power_Control><Power>On</Power></Power_Control></Zone_2></YAMAHA_AV>' \"\"")
        logInfo("test", results)

That last line is the log statement. The script is immediately returning with no output.

You can try running it from the commands line as openHAB and see if you get any output.

sudo -u openhab command

You can also try and add the -v (verbose) argument to the curl. It will show what the headers are that are returned so you can tell what sort of error you are getting.

you can quickly smoke out if it is a permissions problem by running openHAB as root temporarily

So weird, If I run the exact command using ‘sudo -u openhab’ from the command line the command works as expected, when I ran the verbose option i didnt see any additional logging in karaf.

given the above is it still worth trying to run openhab as root? if so how would I do that under a linux installation?

Just got it working! I saw another post where you had suggested to use the execcommand with a timeout value. As soon as I added that the command now works.

Thanks for the help everyone.

thread is older but maybe bether as to start a new one about same issue/question…

I install the exec binding, restart openHAB.
Than I test as user openhab this command on shell, it works: Message was send.

/usr/bin/curl -s --form-string 'token=anvF8xxxxxxxxxxxxzsE' --form- string 'user=uSMJDxxxxxxxxxxxxxxxxxxxS' --form-string ' TEST Message' https://api.pushover.net/1/messages.json

But when put this in the rule, the line is executed, but no message will send.
Also no error on log, also loginfo/results for the curl comamnd is empty.
(logInfo “ALARM !!!” is written)

if( Alarmswitch.state == CLOSED )
       logInfo("contact.rules", "!!! ALARM !!! ")
       val results = executeCommandLine("/usr/bin/curl -s --form-string 'token=anvF8xxxxxxxxxxxxzsE' --form- string 'user=uSMJDxxxxxxxxxxxxxxxxxxxS' --form-string ' TEST Message' https://api.pushover.net/1/messages.json")
logInfo("contact.rules", "curl output/return: ", results) 

You have to give executeCommandLine a second parameter telling it the maximum number of milliseconds to wait for the command to finish executing. The command must return within that amount of time or there will be no result.

Without the timeout, it runs the script in the background, the call immediately returns, and there will be no result returned from the command.

I add at thhe end:
… et/1/messages.json",5000)

but still the same:
No error in log, also the logInfo “resultes” is still empty.

When sending the curl-command from Linux shell (as user openhab) the message was send successfully in less than 1 second: (I receive the message on the smartphone),
and on the shell there is the message (output):


Wondering, why from openHAb - rules / executeCommandLine it will not work.
Maybe a (stupid) beginner mistake form my side, because new in opeHAB and new with “executeCommandLine” …

If you copied and pasted this, note that it’s missing a closing ". That might cause problems.

How did you test as user openhab?

The missing " was a paste mistake, this is “only” the return from succesfull shell command.

I test it on the Shell with:

# sudo su openhab curl -s --form-string 'user=uSMJDYxxxxxis' --form-string message='Test ...'  https://api.pushover.net/1/messages.json

Just found that there is an add-on -action to use Pushover (what my curl part to execute) directly, and this works.
( https://www.openhab.org/addons/actions/pushover )

However, will be interest, why “my” curl will not work as exepected in the rule.
I can test further if new hints received (actuall the curl-code is only commented out).