Hi there,
as I’ve got some issues after upgrading to openHAB 2.5.6 (some bindings are always “RESOLVED” after restarting the openHAB service and won’t come “ACTIVE” in order to run properly), I came up with a solution for this I want to share with you.
As the only way to restart a binding is via KARAF’s bundle management we need to tackle the fact that it needs a password. Of course we’re approaching an automated solution where there’s no user interaction whatsoever.
Therefore you need to install sshpass first;
apt-get update
apt-get install sshpass
If you like, you can already run a first test (the default <YOUR_SSH_PASS> can be found here) and check the installed bindings;
sshpass -p "<YOUR_SSH_PASS>" ssh -p 8101 openhab@localhost bundle:list
If that works, you’re halfway there!
Next, we’re going to set up a shell-script (… in order to run it regularly via crontab - but more on that later on). Choose a path <YOUR_PATH> on your machine that suits your needs, create a file and name it <YOUR_SCRIPT>.sh. Of course it may be improved, but for now it does the work!
#!/bin/bash
# get all openhab bindings that are "resolved"
resolved=$(sshpass -p "<YOUR_SSH_PASS>" ssh -p 8101 openhab@localhost bundle:list | grep 'Resolved')
# get the length of $resolved in order to determine if any resolved bindings have been found
resolved_size=${#resolved}
# do the magic if resolved bindings have been found
if [ $resolved_size -gt 0 ]
then
# loop through all the resolved bindings, line-by-line
while IFS= read -r line; do
# this is the whole line
binding=$line
# this will be the resolved binding's id
binding_id="${binding:0:3}"
# do some output, just in case...
echo "Sending SSH command to restart Binding ${binding_id}"
# send command
sshpass -p "<YOUR_SSH_PASS>" ssh -p 8101 openhab@localhost bundle:restart "${binding_id}"
done <<< "$resolved"
else
echo "No resolved bindings found. Everything's fine!"
fi
Save the file and run the following command on it, so it becomes actually executable.
chmod +x <YOUR_SCRIPT>.sh
If you like, you can run the script and see if it works - browse to <YOUR_PATH> and set command;
<YOUR_SCRIPT>.sh
If everything works and all bindings are ACTIVE, the output will look like;
Password authentication
No resolved bindings found. Everything's fine!
Last, but not least, you want to add a cronjob for this via;
crontab -e
And putting a new line (e.g. that’ll run the script every minute, but feel free to set this however you need it - a “translator” can be found here);
* * * * * <YOUR_PATH>.<YOUR_SCRIPT>.sh
That’s all!