Request for tested 'exec:command:' example

My Problem and Appreciation

I’m trying to implement a exec:command thing which I’m not able to get working.
To determine what the problem is, the installed OpenHab or just my incompetence I need this example.

I appreciate anyone’s serious effort to help me out with this.

So far

I have tried all of the solutions from the OH manual, this forum and other sources for days now.
Nothing seems to work.
Not even some errors show up in the log.
(Maybe needles to say I’m not a novice in Linux and programming)

What is working, is calling the bash script from a rule using function executeCommandLine().

What I Need

I need the simplest and tested example containing files as:

  • example.things
  • example.items
  • example.rules
  • example.sh (/user/local/bin)

The bash script I’m using is:

#!/bin/bash

# Write to a file which I run a tail on to check interaction from OpenHab.
echo "Call: $1 $2"  >> "/tmp/output.txt"

# Return a static value for now.
echo "OK"

My OpenHab Version Info

openHAB Distribution Version Information
----------------------------------------
build-no        : Release Build
online-repo     : https://openhab.jfrog.io/openhab/libs-release

Repository        Version
----------------------------------------
openhab-distro  : 2.5.11
openhab-core    : 2.5.0
openhab1-addons : 1.14.0
openhab2-addons : 2.5.11
karaf           : 4.2.7

Did you follow the documentation? You did not post amy whitelist configuration.

But of course I did :slight_smile:

It is not exactly clear what the format is in this whitelist file.
I assumed it was the full path without any arguments.

Since you tried it and have it working of course.
Can you tell me if omitting a whitelist entry will generate an notification of some sort in the log?

I just know it is required. I do not use that binding.

As I reported in my initial post I followed the manual and this forum’s topics about it and the white listing are part of it.

Go through the thread below and download some of my files - absolutely loads of exec examples before I made a binding. Most of the issues are due to white spaces and there is one situation where the exec binding fails completely (this was opened on git but don’t think it was ever fixed).

There are examples in there for straight up exec, and also the formatting where you want to create another file from exec (lots of escapes).

Hopefully it helps as it took me months to fathom it out for certain situations

Have you tried also calling the script / commands from a terminal as user openhab? Permissions are a big show stopper

If you have it wrong it’ll complain about the command not being in the white list.

The entry needs to exactly match the command configured on the Thing, including arguments and the %2$s and such (see the third example in the docs: php ./configurations/scripts/script.php %2$s).

Yes, but I don’t have an example handy. It’ll say something like “tried to execute command blah blah blah that isn’t in the whitelist.”

As I said I do not see any errors about missing an entry in a whitelist in the log.

But do you have a tested example you can share with me?

Yes of course I tried and it works.
Since executeCommandLine() is working fine, permissions can never be my problem.

I read your reference to the forum topic containing downloadable files wit lots of calls to executeCommandLine() but no definition of Thing using exec:command:.

Sorry this was not helpful at all.

did you install the binding? I don’t have an example. I don’t use the binding.

These work, example including parameters for script

Thing exec:command:LwAuth 		"Authentication" 	@ "Cupboard" 	[ command="%2$s", interval=3600,timeout=10, autorun=true ]
Thing exec:command:LwGet 		"Update" 		@ "Cupboard" 	[ command="sh /share/QPKGs/OpenHab/conf/scripts/Lw_Get.sh %2$s", interval=20,timeout=19, autorun=true ]
Thing exec:command:LwUpdate_Structures 	"Structures" 		@ "Cupboard" 	[ command="sh /share/QPKGs/OpenHab/conf/scripts/Lw_Update_Structures.sh %2$s", interval=0,timeout=10000, autorun=true ]


#!/bin/bash
curl -s -X POST https://publicapi.lightwaverf.com/v1/feature/$1 -H Content-Type:application/json -H "Authorization:bearer $2" -d '{"value":"'$3'"}'

And there is an example of creating a things file from a rule in lots of the files in the thread:

//Define Things File

var String RulesFile = 	"Thing exec:command:LwAuth [ command=\\\"%2\\$s\\\", interval=3600,timeout=5, autorun=true ]\r\n" +
			"Thing exec:command:LwGet [ command=\\\"sh " + OpenHabScriptsPath + "Lw_Get.sh %2\\$s\\\", interval=" + RefreshRate + ",timeout=" + RefreshRateTimeout + ", autorun=true ]\r\n" +
			"Thing exec:command:LwUpdate_Structures [ command=\\\"sh " + OpenHabScriptsPath + "Lw_Update_Structures.sh %2\\$s\\\", interval=0,timeout=1000, autorun=true ]\r\n" +
			"Thing exec:command:LwUpdate_Features [ command=\\\"sh " + OpenHabScriptsPath + "Lw_Update_Features.sh %2\\$s\\\", interval=0,timeout=1000, autorun=true ]\r\n" +
			"Thing exec:command:LwUpdate [ command=\\\"sh " + OpenHabScriptsPath + "Lw_Update.sh %2\\$s\\\", interval=0,timeout=1000, autorun=true ]\r\n"
//Create Things File
var File5 = executeCommandLine("sh@@-c@@echo \"" +  RulesFile  + "\" > " + OpenHabThingsPath + "Lw.things",5000)
logInfo("LwSetup - File 5",File5)

Lw_Setup.sendCommand(OFF)
LwUpdate.sendCommand(OFF)
}
end

Got it to Work (Pfffft!!) \o/

Cause: A broken/non persistent console feature:install command that apparently does not update the addons database or RTFM applies to me too.

What I Noticed

I’m sure I installed openhab-binding-exec but for some reason it got uninstalled.

Console Result (feature:list | grep ‘exec’`)

openhab-binding-exec                              │ 2.5.11           │          │ Uninstalled │ openhab-addons-2.5.11    │ Exec Binding
openhab-transformation-exec                       │ 2.5.11           │ x        │ Started     │ openhab-addons-2.5.11    │ Exec Transformation

So I installed it again using the console command

feature:install openhab-binding-exec

After that I watched the logs ignoring the zwave lines using this command.

openhab-cli showlogs | grep -iv zwave

Look at the last line “THE FEATURE GETS AUTO UNINSTALLED!!”

==> /var/log/openhab2/openhab.log <==
2020-12-17 20:54:50.983 [DEBUG] [c.internal.ExecWhitelistWatchService] - Updated command whitelist: [, /usr/local/bin/example.sh %2$s]

==> /var/log/openhab2/events.log <==
2020-12-17 20:54:50.998 [hingStatusInfoChangedEvent] - 'exec:command:example' changed from UNINITIALIZED to INITIALIZING
2020-12-17 20:54:51.023 [hingStatusInfoChangedEvent] - 'exec:command:example' changed from INITIALIZING to ONLINE

==> /var/log/openhab2/events.log <==
2020-12-17 20:55:01.268 [vent.ItemStateChangedEvent] - SF_String changed from 2020-12-17 20:54 to 2020-12-17 20:55
2020-12-17 20:55:01.472 [vent.ConfigStatusInfoEvent] - ConfigStatusInfo [configStatusMessages=[]]
2020-12-17 20:55:01.477 [vent.ConfigStatusInfoEvent] - ConfigStatusInfo [configStatusMessages=[]]
2020-12-17 20:55:01.496 [vent.ConfigStatusInfoEvent] - ConfigStatusInfo [configStatusMessages=[]]
2020-12-17 20:55:01.505 [vent.ConfigStatusInfoEvent] - ConfigStatusInfo [configStatusMessages=[]]
2020-12-17 20:55:01.506 [vent.ConfigStatusInfoEvent] - ConfigStatusInfo [configStatusMessages=[]]

==> /var/log/openhab2/events.log <==
2020-12-17 20:55:20.310 [hingStatusInfoChangedEvent] - 'exec:command:example' changed from ONLINE to UNINITIALIZED
2020-12-17 20:55:20.314 [hingStatusInfoChangedEvent] - 'exec:command:example' changed from UNINITIALIZED to UNINITIALIZED (HANDLER_MISSING_ERROR)
2020-12-17 20:55:20.406 [thome.event.ExtensionEvent] - Extension 'binding-exec' has been uninstalled.

What am I missing here? Am I tripping over a bug again?
So I added all bindings in the addons.cfg configuration file and that kept the binding addon to be installed.

So My Tested Example Configuration

This for the prosperity of openhab2 users not struggling for days by just finding this post.

File: services/addons.cfg (most important!!!)

binding = zwave,astro,avmfritz,network,http1,exec

File: things/example.things

Thing exec:command:example "Example Handler" @ "Home" [ command="/usr/local/bin/example.sh %2$s", interval=0,timeout=10, autorun=true ]

File: items/example.items

// Arguments to be placed for '%2$s' in command line
String ExampleInput "Example Args" { channel="exec:command:example:input" }
// Output of command line execution 
String ExampleOutput "Example Output" { channel="exec:command:example:output" }
// Switch to trigger the rule "Example trigger handler".
Switch ExampleTrigger "Example Trigger"

File: rules/example.rules

rule "Example trigger handler"
when
	Item ExampleTrigger changed
then
	if (ExampleTrigger.state == ON)
		ExampleInput.sendCommand("ON")
	else
		ExampleInput.sendCommand("OFF")
end

File: misc/exec.whitelist

# For security reasons all commands that are used by the exec binding or transformation need to be whitelisted.
# Every command needs to be listed on a separate line below.
/usr/local/bin/example.sh

File: /usr/local/bin/example.sh

#!/bin/bash
echo "Call: $1 $2"  >> "/tmp/output.txt"
echo "OK"

Watch the file to see if script is called at all using:

tail -F /tmp/output.txt`

Test access by openhab user using:

sudo -u openhab /usr/local/bin/example.sh "Call using sudo"

Openhab Log (Lines of Joy!!!)

openhab-cli console log:tail

Result command ‘ON’.

21:40:35.168 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'ExampleTrigger' received command ON
21:40:35.170 [INFO ] [smarthome.event.ItemStateChangedEvent] - ExampleTrigger changed from OFF to ON
21:40:35.171 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'ExampleInput' received command ON
21:40:35.173 [INFO ] [arthome.event.ItemStatePredictedEvent] - ExampleInput predicted to become ON
21:40:35.175 [TRACE] [ing.exec.internal.handler.ExecHandler] - Executing command '/usr/local/bin/example.sh %2$s' after a change of the input channel to 'ON'
21:40:35.176 [DEBUG] [ing.exec.internal.handler.ExecHandler] - Passing to shell for parsing command.
21:40:35.177 [DEBUG] [ing.exec.internal.handler.ExecHandler] - OS: *NIX (Linux)
21:40:35.177 [TRACE] [ing.exec.internal.handler.ExecHandler] - The command to be executed will be '[sh, -c, /usr/local/bin/example.sh ON]'
21:40:35.175 [INFO ] [smarthome.event.ItemStateChangedEvent] - ExampleInput changed from OFF to ON
21:40:35.181 [DEBUG] [ing.exec.internal.handler.ExecHandler] - Exec [OUTPUT]: 'OK'
21:40:35.182 [DEBUG] [ing.exec.internal.handler.ExecHandler] - Transformed response is 'OK'

Result command ‘OFF’.

21:40:40.183 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'ExampleTrigger' received command OFF
21:40:40.185 [INFO ] [smarthome.event.ItemStateChangedEvent] - ExampleTrigger changed from ON to OFF
21:40:40.187 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'ExampleInput' received command OFF
21:40:40.189 [INFO ] [arthome.event.ItemStatePredictedEvent] - ExampleInput predicted to become OFF
21:40:40.193 [INFO ] [smarthome.event.ItemStateChangedEvent] - ExampleInput changed from ON to OFF
21:40:40.193 [TRACE] [ing.exec.internal.handler.ExecHandler] - Executing command '/usr/local/bin/example.sh %2$s' after a change of the input channel to 'OFF'
21:40:40.194 [DEBUG] [ing.exec.internal.handler.ExecHandler] - Passing to shell for parsing command.
21:40:40.194 [DEBUG] [ing.exec.internal.handler.ExecHandler] - OS: *NIX (Linux)
21:40:40.195 [TRACE] [ing.exec.internal.handler.ExecHandler] - The command to be executed will be '[sh, -c, /usr/local/bin/example.sh OFF]'
21:40:40.202 [DEBUG] [ing.exec.internal.handler.ExecHandler] - Exec [OUTPUT]: 'OK'
21:40:40.204 [DEBUG] [ing.exec.internal.handler.ExecHandler] - Transformed response is 'OK'
1 Like

Just to add a bit more explanation.

If you use addons.cfg at all, you can only install new add-ons by adding them to addons.cfg. OH periodically reads that file and if it sees the list of add-ons it has installed is different from the list in addons.cfg, it will install/uninstall them to make the list match.

You cannot install some bindings using addons.cfg and others through the UI. It’s all or nothing.

1 Like

I’m aware that bindings in addons.cfg overrules the regular binding configuration.

But is my assumption that the command feature:install is not working as expected, correct?
Is there anybody who has the same experience?

feature:install will also be overwritten by addons.cfg

Of course addon.cfg has highest priority.
Putting my bindings in the empty addon.cfg resolved my problem.
Even after I restored addon.cfg (empty) the exec binding was persistent.

I’m sure the feature:install command is broken (empty addons.cfg) or I have missed something in the manual pages.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.