One of the most common problems I help people with on this forum is dealing with the Exec binding. So I’m writing this post so I can reference it later.
When openHAB is installed in the automated way (i.e. via apt-get) an openhab user is created and the service runs under that user. The openhab user has the following attributes:
- no shell so one cannot log in or su to this user
- limited rights
- not a member of sudoers
- has a home directory in /var/lib/openhab (or /var/lib/openhab2)
- has no PATH environment variable beyond the system default
These are the usual sources of all Exec binding errors.
#The Exec Binding
The Exec Binding also has some of its own quirks.
- it does not report success or failure of the command in the logs
- it does not log the results of the command to the logs
- sometimes for technical reasons it requires replacing spaces with
The above can also be a source of errors as well as limit your ability to diagnose and fix problems.
How to Debug
Some easy steps:
If the command or the called script calls
sudo, make sure openhab is a member of sudoers and is configured with NOPASSWORD
Use the full path for all commands called by the binding and any commands called by any script called by the binding
Make sure the openhab user has permissions on all files that are called directly by the binding or called by a script that the binding calls.
Replace spaces with
If these steps do not work, we need to get some more information from the command, in particular we need to get the text that is generated when the command executes.
Remove the exec binding config or link to the exec Channel (if using OH 2) from the Switch Item.
Create a new rule that triggers when the Switch receives a command
In this rule use
val results = executeCommandLine(cmd, timeout)followed by logging out
val results = executeCommandLine("/bin/echo@@'The quick brown fox jumped over the lazy dog.'", 5000) logInfo("execTest", results)
Only executeCommandLine that takes a timeout argument will wait for the command (up to timeout milliseconds) to finish and return any text generated by the script.
- Trigger the Switch and watch the logs for errors.
Common errors I’ve seen include:
Trying to ssh but the host is not part of the openhab user’s known hosts. Either copy the .ssh folder from your usual account to ~/openhab/.ssh (changing permissions and ownership of course) or run
sudo -u openhab /usr/bin/ssh user@hostand typing “yes” when ssh asks if you want to accept the ssh key.
Trying to ssh to a host without setting up certs for the openhab user. I really really recommend against trying to pass the password on the command line. Setting up an rsh keypair is so easy and once set up you never have to type in a password again. But if you neglect to generate a new keypair for the openhab user or copy your main keypair (the key files now become your password, protect accordingly) the openhab user will not be able to log into the remote host.
Incorrect permissions on the files in ~openhab/.ssh. ssh will not connect if the private key file is group or world readable.
Using sudo but failing to add the openhab user to sudoers with NOPASSWORD. If you are on the raspbian probably the easiest thing to do is run
sudo visudoand copy what is there for the pi user and change “pi” to “openhab”.
“Command not found”, check your paths and permissions.