[SOLVED] Help with the exec binding

execbinding
Tags: #<Tag:0x00007f1e5a8c94a0>
(Ionut) #1

Hi,
I am trying to create an exec binding to shut down one of my computers via ssh. Basically what I have done so far is a script that logs onto the machine and runs a “shutdown now” command. The script is correct as I have tested it manually. The script is located in the /home/openhabian folder on my RPi 3.

I added (via PaperUI) a Thing for the shutdown command as follows:

I have linked an item (also via PaperUI) with channels and everything and my items looks now as follows:

Unfortunately in the Control panel I see that the switch is greyed out but I have no idea why.

Could anybody help with this, please ?

Thank you !

0 Likes

(Vincent Regaud) #2

The running channel will be ON when the script is running.
Go through the docs of the binding again

0 Likes

(Ionut) #3

Well… I would like to run the script myself. I’m not interested in finding out when the script is executed.

As for the documentation, believe me - I’ve studied it a lot. Everybody is saying that PaperUI is the future but the examples are still taking files into consideration. Besides, I couldn’t find a simple example to do exactly what I wanted, so this is why I came here.

0 Likes

(Michael) #4

What is the name of your script?
Is it ./shutdown and works with the parameters root and 192.168.1.10 as the destination?
Maybe it´s helpful to post the complete script to have an better understanding.

Maybe you could post what the execution when started manually from the shell.

Rich wrote a Guide to help when having problems with the exec binding, maybe it´s worth a look?

It may be the future, but now in the present :wink:
There are still many things that are only possible with files and not through PaperUI.

0 Likes

(Ionut) #5

Hi Michael,
Thank you for your detailed answer. Indeed, now I noticed that I had forgot the suffix for my script (it was shutdown.sh instead of shutdown).

[14:58:37] openhabian@openHABianPi:~$ ls
README.txt  reboot.sh  shutdown.sh

[15:01:41] openhabian@openHABianPi:~$ pwd
/home/openhabian

The script is nothing complex, really:

[14:57:11] openhabian@openHABianPi:~$ cat shutdown.sh 
#!/bin/bash
echo "`date`: shutdown : $1 $2" >> /tmp/shutdown_script.log 2>&1
ssh $1@$2 /sbin/shutdown now >> /tmp/shutdown_script.log 2>&1

I changed the Thing to refer to the correct script now. Now my Thing looks as follows:

However, this doesn’t help too much as I still cannot run the script from the Control panel.

When I run the script locally, I get nothing as the output is redirected to the log file:

[15:01:47] openhabian@openHABianPi:~$ ./shutdown.sh root 192.168.1.10

In the log file I get the results of the shutdown operation. My kodi is turned off now and I don’t have access to it to restart it (this is why it shows up as not accessible). :slight_smile: However, it’s clear that the script functions correctly.

[15:05:41] openhabian@openHABianPi:~$ cat /tmp/shutdown_script.log 
Tue Jan  8 15:18:42 EET 2019: shutdown : root 192.168.1.10
ssh: connect to host 192.168.1.10 port 22: No route to host

For some reason I didn’t find Rich’s guide until now. I will try to read it thoroughly to see if it can help.

0 Likes

(Ionut) #6

Ok, I was able to fix it. What I did to make it work:

  1. I used the full path to the script (/home/openhabian/shutdown.sh instead of ./shutdown.sh)
  2. I gave write permissions to the /tmp/shutdown_script.log file to everybody (even though the file was owned by openhabian, the script couldn’t write to it).

Now, when I click on the Kodi Shutdown control, it temporarily changes to ON and then to OFF. I will have to modify it a little to show the actual status of the Kodi box, but for now it’s fine.

I still see a warning in the logs and I’m not sure where I should fill in those details:

15:44:03.924 [TRACE] [nhab.binding.exec.handler.ExecHandler] - The command to be executed will be '/home/openhabian/shutdown.sh root 192.168.1.10'
15:44:03.934 [INFO ] [smarthome.event.ItemStateChangedEvent] - KodiShutdown_Running changed from OFF to ON
15:44:07.092 [WARN ] [al.profiles.ExecTransformationProfile] - Please specify a function and a source format for this Profile in the 'function' and 'sourceFormat' parameters. Returning the original state now.
15:44:07.115 [DEBUG] [nhab.binding.exec.handler.ExecHandler] - Transformed response is ''
15:44:07.120 [INFO ] [smarthome.event.ItemStateChangedEvent] - KodiShutdown_Running changed from ON to OFF
1 Like

(Udo Hartmann) #7

Please be aware that the user, which runs openHAB is openhab, not openhabian
The home directory of user openhab is /var/lib/openhab2/ (when using openHABian).

0 Likes

(Ionut) #8

Thank you !
In the meantime I noticed that my script, even if it works from the command line, it doesn’t work from the interface button. I even realized why: it seems that my certificate has been generated in the openhabian account and, since the application is executed with the openhab user, it cannot login (“Host key verification failed”).

Now, how can I generate the keys for the openhab user as well ? I couldn’t find any help page on that, sorry if such a subject has been already discussed.

0 Likes

(Ionut) #9

Ok, I figured it out. I had to (temporarily) modify in the /etc/passwd the shell for the openhab user from /bin/false to bin/bash.

0 Likes

(Udo Hartmann) #10

You could have used su to login as openhab:

sudo su -s /bin/bash openhab

:slight_smile:

1 Like

(Ionut) #11

I didn’t know about the “–shell” option. You helped me learn something new today ! Thank you !

0 Likes

(Udo Hartmann) #12

You’re welcome :slight_smile:

0 Likes