[Solved]Exec Binding does nothing on executeCommandLine

Hello I’m having an issue where openhab is not executing a script. It says it did, but nothing happens.
I’ve already searched around:
https://community.openhab.org/t/problem-getting-exec-binding-to-execute-command
https://community.openhab.org/t/solved-problems-with-exec-binding
But neither one seems to help ( or I just missed it)

The details:

Making sure exec is installed : sudo apt-get install openhab-addon-binding-exec

openhab-addon-binding-exec is already the newest version.

The Item

Switch extTest2 “execute Test2”

The Rule

import org.openhab.core.library.types.*
rule Exec2
when
Item extTest2 received command
then
logInfo(“extTest2”, “extTest2 switch was turned " + receivedCommand.toString)
if (extTest2.state == ON) {
executeCommandLine(”/home/pi/bin/MCRemote start")
}
else {
executeCommandLine(“/home/pi/bin/MCRemote stop”)
}
end

Because this forum refuses to let me indent anything here is a pastbin of the code:

Exec Binding does nothing on executeCommandLine code - Pastebin.com

OpenHab log when toggling the extTest2 switch

2016-03-18 13:45:08.029 [INFO ] [.openhab.model.script.extTest2] - extTest2 switch was turned OFF
2016-03-18 13:45:08.325 [INFO ] [g.openhab.io.net.exec.ExecUtil] - executed commandLine ‘/home/pi/bin/MCRemote stop’
2016-03-18 13:45:10.327 [INFO ] [.openhab.model.script.extTest2] - extTest2 switch was turned ON
2016-03-18 13:45:10.380 [INFO ] [g.openhab.io.net.exec.ExecUtil] - executed commandLine ‘/home/pi/bin/MCRemote start’

The script MCRemote {start,stop} works exactly how it should when called from command line.
permissions: -rwxr-xr-x 1 pi openhab 759 Mar 18 12:54 MCRemote

Any tips and advice would be appreciated as i’m kinda stuck.

Since you used apt-get to install the addon, I take it that you installed openHAB itself with apt-get, so the user, under which openHAB is running is openhab. But if the script resides in /home/pi/bin, this will be only readable for pi (and root) but not for openhab. Please take a look at ls -l /home/

I suggest to put the script to a more appropriate place like /etc/openhab/configurations/scripts/

:wink:

The permissions should be enough but the script folder is a better location.

I’ve put he script in the scripts folder and changed permissons

-rwxr-xr-x 1 openhab openhab 759 Mar 18 12:54 MCRemote.sh
Note I appended .sh to the script name and in the rule file

Nothing seems to change though. Same behavior.

I came across a suggestion to use @@ between arguments like

executeCommandLine(“/etc/openhab/configurations/scripts/MCRemote.sh@@start”)

Again nothing seems to change

Highlight your code and press the </> button.

Or indent your lines by at least four spaces

Or put three back ticks ` at the start and end of your code block

```
your code
    indented
```

You can even get syntax highlighting if you specify the language after the three backticks (use java for Rules)

    ```java
    import org.openhab.core.library.types.*
    rule Exec2 
    when
        Item extTest2 received command
    then
    logInfo("extTest2", "extTest2 switch was turned " + receivedCommand.toString)
        if (extTest2.state == ON) {
            executeCommandLine("/home/pi/bin/MCRemote start")
        }
        else {
            executeCommandLine("/home/pi/bin/MCRemote stop")
        }
    end
    ```

NOTE: until the latest changes to the setpermissions.sh script get released your scripts in /etc/openhab/configurations/scripts will have their permissions changed which strips the execute permissions off of them. Grrr. I just added an executeCommandLine to chmod it back before calling it but it is something to keep in mind.

Change your call to the following and review the logs to see what text is being produced when you call it.

val String results = executeCommandLine("/home/pi/bin/MCRemote start", 5000)
logInfo("Exec2", results)

If you have a permission error or some other error you should now see the error text printed in your logs.

NOTE: if your MCRemote script involves an ssh and you have keys set up so you don’t need to use passwords, not only do you need to add those keys to ~openhab/.ssh but the setpermissions.sh script will change the permissions on those files as well. Everything in the .ssh folder needs to be user rw only or else ssh will give you an error.

1 Like

yay that got it to at least run the script
Yes the script contains an ssh call but since I couldnt get the public key sent to the server nor work, I just used the sshpass varient. Its on the same home network so I hasnt been a primary issue yet. (Its an error I’ll tackle at a latter date)

This is the main meat of MCRemote.sh. It is now displaying the echos then displays nothing.

#thanks for the syntax and intent advice :)
NOW=`date "+%Y-%m-%d_%Hh%M"`
COMMAND=MCstartscript_3301 #the script called on the host server

mc_start() {
	echo "starting"
	echo "line 2 test"
	echo `sshpass -p mypass ssh minecraft@192.168.2.210 ./$COMMAND command "/say hi start $NOW"` 
	echo "start $NOW" >> MCRemote.log
}

mc_stop() {
	echo "stopping"
	echo "line 2 test"
	#echo `sshpass -p mypas ssh minecraft@192.168.2.210 ./$COMMAND stop $NOW`
	echo `sshpass -p mypass ssh minecraft@192.168.2.210 ./$COMMAND command "/say hi stop $NOW"`
	echo "stop $NOW" >> MCRemote.log
}

The updated rule

rule Exec2
when
  Item extTest2 received command
then
  logInfo("extTest2", "extTest2 switch was turned " + receivedCommand.toString)
  if (extTest2.state == ON) {
	//val String results = executeCommandLine("/etc/openhab/configurations/scripts/MCRemote start", 5000)
	val String results = executeCommandLine("/home/pi/bin/MCRemote.sh@@start", 5000)
	//val String results = executeCommandLine("MCRemote.sh@@start", 5000)
	logInfo("Exec2", results)
  }
  else {
	//val String results = executeCommandLine("/etc/openhab/configurations/scripts/MCRemote stop", 5000)
	val String results = executeCommandLine("/home/pi/bin/MCRemote.sh stop", 5000)
	//val String results = executeCommandLine("MCRemote.sh stop", 5000)
	logInfo("Exec2", results)
  }
end

only the non commented version works. Also curiously the @@ has no affect

Output

2016-03-18 23:32:00.369 [INFO ] [.openhab.model.script.extTest2] - extTest2 switch was turned OFF
2016-03-18 23:32:00.585 [INFO ] [org.openhab.model.script.Exec2] - stopping
Host key verification failed.

2016-03-18 23:32:10.244 [INFO ] [.openhab.model.script.extTest2] - extTest2 switch was turned ON
2016-03-18 23:32:10.418 [INFO ] [org.openhab.model.script.Exec2] - starting
line 2

2016-03-18 23:32:15.246 [INFO ] [.openhab.model.script.extTest2] - extTest2 switch was turned OFF
2016-03-18 23:32:15.416 [INFO ] [org.openhab.model.script.Exec2] - stopping

Once there was the “Host key verification failed” warning but It only shows up rarely, and im not using keys

For reference the output of MCRemote when ran from command line should be:

pi@openhabpi:~/bin $ MCRemote.sh start
starting
line 2 test
MCstartscript_3301 For ResonantRise Minecraft server. minecraft_server_ResonantRise3301 is running… executing command [23:46:54] [Server thread/INFO]: [Server] hi start 2016-03-18_23h46

I had tried to do something similar - use a rule to ssh into a remote machine to play some audio. I went down this same path and had most of the same issues you are describing here. When I finally got it working, I didn’t like the performance. There is lag waiting for the remote machine to accept the ssh connection. That lag is kind of variable, and if you are trying to use the audio as a cue to the user that something has taken place, the timing is everything.

I know this doesn’t answer your question directly, but what I ended up doing was installing opehHAB also on my remote machine and installed the MQTT binding - connecting it to the MQTT broker on my main machine (the config of that broker needs to allow connections from outside of localhost). On the main machine, rather than having my rule execute a script to ssh, I have my rule publish to the MQTT bus, and the openHAB on the remote machine has an item that is listening on the MQTT bus and plays that audio when it gets triggered. The resulting performance is near realtime playing of the audio. MUCH improved. I also feel that it is probably more secure. With ssh the openHAB user ends up with a ssh key that really allows full access to the remote machine. In this scheme the openHAB user really has no additional access to the remote machine above what publishing to the MQTT bus can do.

Even though you are not using keys to log in, the host keys are still being used. Each host has its own key and the first time you ssh connect from one host to another as a user ssh will ask whether you should trust that host’s key it not. These host keys are what are used to set up he encryption.

The error indicates there is some problem with the host keys. You can see whether copying the trustes-hosts file from your login to ~openhab/.ssh, changing ownership to openhab:openhab.

Then increase the timeout on the call from 5000 to 10000. You are intermittently seeing the error probably because five seconds is to short a time to wait for the call to fall. L

That did it!

I went and re-setup the keys for openhab by su openhab and running the needed commands. After that It worked!

Thank you very much :slight_smile:

Although I am curious why the @@ has no effect here. Is it not needed? I’ve read that it’s needed to separate arguments. What scenario are those needed?

then
  logInfo("extTest2", "extTest2 switch was turned " + receivedCommand.toString)
  if (extTest2.state == ON) {
	//val String results = executeCommandLine("/etc/openhab/configurations/scripts/MCRemote start", 5000)
	val String results = executeCommandLine("/home/pi/bin/MCRemote.sh@@start", 15000)
	logInfo("Exec2", results)
  }
  else {
	//val String results = executeCommandLine("/etc/openhab/configurations/scripts/MCRemote stop", 5000)
	val String results = executeCommandLine("/home/pi/bin/MCRemote.sh stop", 15000)
	logInfo("Exec2", results)
  }
end

I think it is sometimes needed when you use the Exec binding but not needed when you use the executeCommandLine, and it is’t always needed for the Exec Binding. The documentation on the wiki basically says if it doesn’t work with spaces replace the spaces with “@@”.

Hi, I was wondering how you got the audio to play on the remote machine listening for the trigger on the MQTT channel ?

Many Thanks