How to reboot Raspberry Pi 3 B+ by Openhab2 Switch

I try to reboot my Pi using a Switch on Openhab2.
I created a file /var/www/html/ResetPi.sh that contain the command 'reboot’
Then I used:

sudo chown root:root /var/www/html/ResetPi.sh
sudo chmod 700 /var/www/html/ResetPi.sh

Using Visudo I wrote the line: openhabian ALL=(ALL) NOPASSWD: /var/www/html/ResetPi.sh
I create a rule when a Switch is changed to ON then:
var String response3 = executeCommandLine(“sudo /var/www/html/ResetPi.sh”,1000)
And I receive the response:

*We trust you have received the usual lecture from the local System*
*Administrator. It usually boils down to these three things:*

*    #1) Respect the privacy of others.*
*    #2) Think before you type.*
*    #3) With great power comes great responsibility.*
*sudo: no tty present and no askpass program specified*

If in that rule I try :
var String response3 = executeCommandLine(“sudo -u openhab ssh openhabian@192.168.XX.XX sudo /var/www/html/ResetPi.sh now”,1000)
then I receive the response:
Permission denied (publickey,password)
In Putty if I use the command “sudo /var/www/html/ResetPi.sh” or “sudo reboot”, the Raspberry Pi is rebooting.

What do I do wrong? Thanks!

Did you put the command in the whitelist

Adding the command to the whitelist is not required for executeCommandLine but for the exec binding.
As far as I understand the entry that you made is for the user openhabian.
The OH service runs with openhab privileges so the user openhab needs to be added instead of openhabian in the sudoers file.

I do not know what is whitelist. I put the rule in my file home.rules opened in puddy with:
sudo nano /etc/openhab2/rules/home.rules

Thanks! It worked! Now I tried to use sudoers.d folder. I created this file
sudo nano /etc/sudoers.d/ResetPi.sh
I edit it withreboot
CTRL+X and Yes
sudo visudo
looks like this:
# User privilege specification
root ALL=(ALL:ALL) ALL

# Allow members of group sudo to execute any command
%sudo ALL=(ALL:ALL) ALL
openhab ALL=(ALL) NOPASSWD: /etc/sudoers.d/ResetPi.sh
CTRL+X and Yes
sudo nano /etc/openhab2/rules/home.rules
I create a rule when a Switch is changed to ON then:
var String response3 = executeCommandLine(“sudo /etc/sudoers.d/ResetPi.sh”,1000)
CTRL+X and Yes

1 Like