Problem getting exec binding to execute command

I’m just getting started with OpenHab (1.7.1) running on Ubuntu server 14.04.3. I have created a simple sitemap and item page, and am trying to get the exec binding to work. Right now I have a doorbell mp3 on another ubuntu machine that I am trying to get to play when turning a switch on. below are my sitemap and items.

SiteMap:
sitemap MainMenu label=“Main Menu”{
Frame{
Group item=webcams icon=“webcam” label=“WebCams”{
Video item=Kitchen icon=“webcam” url=“http://webcamurl1/video/mjpg.cgi” encoding=“mjpeg”
Video item=LivingRoom icon=“webcam” url=“http://webcamurl2/video/mjpg.cgi” encoding=“mjpeg”
}
Group item=light_controls icon=“bulb” label=“Light Control”{
Switch item=play_doorbell label=“Doorbell” mappings=[ON=Play]
}
}
}

Items:
Group webcams
Group light_controls
Switch play_doorbell “Doorbell” (light_controls) {exec=“ON:ssh user@host mplayer doorbell.mp3”, autoupdate=“false”}

I have confirmed that I can take the command (ssh user@host mplayer doorbell.mp3) and execute it from the terminal of the OpenHAB server and it successfully plays the mp3 on the other machine. Additionally, I see the data below in the openhab.log file after pressing the switch button.

2015-09-28 12:46:28.101 [INFO ] [.o.b.exec.internal.ExecBinding] - executed commandLine ‘ssh user@host mplayer doorbell.mp3’

I have this binding (org.openhab.binding.exec-1.7.1.jar) in the openhab addons directory.

To me it looks like the command was executed, but the mp3 certainly isn’t playing?? I’m not sure where else to look?

I’m just getting started myself so maybe the way I would do it is not the preferred way but…

I would use executeCommandLine() in a rule or a script to do what you want. First off I assume that the physical door bell will be connected to some kind of interface or be a wireless kind. If it is wireless first you have to create the item for it and it may not be a switch, it may be a sensor, so you would use the Contact item type not a Switch. But that is another topic all together.

I use executeCommandLine() on my Ubuntu 14.04 to execute espeak for some feedback from some actions. I use it in a script and have an item called Response in my Items file. Here is what I have. If you have more questions ask away.

// Items File
    String    Response

// Rules File
rule
when
    Item play_doorbell changed
then
    if (play_doorbell.state == CLOSED) {
        postUpdate(Response,' "Whatever I want to say" ')        
        callScript("espeak")
    }
end
// Script file called espeak.script
    Thread::sleep(1000) // give some time for the Response var to be set
    // set up the variable and replace all the spaces with a dash instead
    var String espeakcommand = "espeak -v en-north -p 25 -s 100 " + Response.state.toString.replace(" ","-")
   // for you it may say "ON:ssh user@host mplayer " + Response.state.toString  // but you would have set it to "doorbell.mp3"

    executeCommandLine(espeakcommand)

// end of Script file

What user is openHAB running under verses what user are you running under when you execute the ssh yourself rom the command line? It could be that if they are different the openHAB user is not configured to log into the other server without supplying a password or the remote machine doesn’t have the openhab user’s keys in its allowed list.

1 Like

i’m using the same user to run openHAB and to execute the script, and as I said I can manually execute this same command from the terminal under the same user account. I’ve configured key authentication for ssh so the command can be executed without a password being entered.

I can give that method a try. Unfortunately I just bricked my openHAB installation fiddling with something else, so I’m going to have to re-install. I would certainly like it to work with the original method, as I find it a bit easier if I want to simply configure a button to execute a script, but I’ll take what I can get at this point. The doorbell is just an example that I’m using to test the exec binding. Ultimately I will likely have a combination of exec commands that will be executed from rules/scripts and possibly others fired by switches/buttons with no physical device associated.

I’ve been looking around and don’t have much advice. I don’t use the exec binding much anymore.

I did find this posting in the old forum which has someone who had the same problem with a simple echo type command and a work around. The workaround is to write a local script that openHAB calls that itself runs the ssh command. No explanation for why this is the case was given and it is an old posting but it might get you where you want to go.

Beyond that, I’ve had different behaviors sometimes when calling things from the command line verses from a program (like Java), especially with ssh. Perhaps you just need to single quote the remote command? I.e. ssh user@host 'mplayer doorbell.mp3'

hmm I may give the single quotes a try. Unfortunately one of the first things I tried was putting the ssh call in a script and trying to execute the script instead, but the results were the same. Thanks for the ideas nevertheless. Maybe something odd happened during my first install, i’ll see if I get the same results after a clean install.

Hi Daniel,

I had exactly the same problem with the exec binding: The info log said that the command was executed, but nothing happend, though I was also able to execute the command using the terminal.

What solved it for me was the following tip from the wiki:

Sometimes the commandLine isn’t executed properly. In that cases another exec-method can be used. To accomplish this please use the special delimiter @@ to split command line parameters.

What might also help is to try executing your command from a small java program (using the Runtime.exec() command). If it works then, it probably should also work in Openhab, otherwise you can read and examine the error messages you get.

This sounds promising, I’ll try it as soon as I can and report back.

I didn’t do exactly what you described, but I used executeCommandLine() to run the script in a rule, but the results were the same. The log shows the command executing, but nothing actually happens??? I’m running out of ideas

I tried separating the command line parameters with @@ but the results were the same with that as well.

You may have already tried this but why don’t you try playing the mp3 on the same computer just as a test. Then at least you will know it maybe has something to do with the way you are accessing the other computer.

Well i haven’t done that exactly. What I did do is change the command to a
simple mkdir command that ran on the openhab server instead of the remote
machine. This didn’t work either even though the log said that it
executed. I suppose I could have a permissions here but i’m not sure how
to know for sure. I did place a script on the server that ran the ssh
command, in /usr/local/bin and tried executing it from there to rule out
permissions.

Did you read this in the openHAB Wiki? I am too much of a novice at Linux to know what it means myself but it looks pertinent to your issue.

“Note: when using ‘ssh’ you should use private key authorization since
the password cannot be read from commandline. The given user should have
the necessary permissions.”

Yes I did. Key auth allows you to authenticate to a remote ssh server
using a private key that is exchanged ahead of time rather than a
password. This is necessary if you’re executing ssh commands in OpenHAB
because there’s no way to pass a password in the exec command. I’ve
confirmed that isn’t the issue and i’ve tried commands that don’t use ssh
at all.

Have you looked at the log files on the target computer (e.g., /var/log/messages, /var/log/syslog on Linux) to see if there are any hints there? From the source code, it appears you’d see an ‘couldn’t execute commandLine’ log message if the Java exec call threw an exception so maybe the problem is on the receiving end.

i’ve tried changing the command to a simple mkdir command to create a folder in the home directory of the user that openhab is running under on the openhab server. That rendered the same results. Do we have others here who have a working exec binding?

This is massively basic, but you do have the exec binding jar in your addons folder, right? It seems odd that you are getting no feedback whatsoever.

Perhaps try running openHAB as root and see if that makes a difference, i.e. to eliminate potential permissions issues?

How can I be sure i’m running as root? I’ve done a clean install while logged in as root. Will that do it? I’m not sure if there are any other steps I need to take? After running the install as root, the results are the same.