[SOLVED] Shutting down Raspberry Pi which runs OpenHAB 2

Tags: #<Tag:0x00007fe05dbf8c78> #<Tag:0x00007fe05dbf89d0> #<Tag:0x00007fe05dbf87c8>

(Daniel Müller) #1

I need to define a rule that calls a “shutdown 0” command via a OpenHAB rule, to turn the RaspberryPi (which runs OpenHAB) off, e.g. by using a switch. How can I do this? Of course the idea for OpenHAB is to run it as 24/7 server, but I think it’s not needed to run e.g. when I’m going on holiday.

(Udo Hartmann) #2

Well, if you really want openHAB to shutdown itself, you have to grant openHAB to execute a sudo shutdown -h now.

(Sebastian) #3


one more idea although I never tried it on a Pi.

But if you don’t want to give OH sudo rights you can use knockd.

You have to install and run the daemon and define a knocking sequence in the config file of knockd. And then you can run a script with the exec binding to do the knocking.

To be honestly I never tried to run everything on the same device but I think it should work. I use this to remote shutdown my Synologys running the daemon and the OH Pi doing the knocking.


(Daniel Müller) #4

I know about that point, but the problem is, that i can’t find a way to do this command via item / rule or anything else… It seems to make problems because of missing user rights to run the shutdown.

(Udo Hartmann) #5

with exec1 Binding I would try this:

Switch piShutdown {exec="/usr/bin/sudo /bin/shutdown -h now"}

or with exec2 Binding:

Thing exec:command:TpiShutdown "Pi shutdown" [command="/usr/bin/sudo /bin/shutdown -h now",autorun=false]

and an item corresponding to that thing:

Switch piShutdown {channel="exec:command:TpiShutdown:run"}

Switching the Switch to ON should execute the command. You will have to add the user openhab (given that this is the user which started openHAB) to the file /etc/sudoers like that:

# User privilege specification
root    ALL=(ALL:ALL) ALL
openhab    ALL = NOPASSWD; /bin/shutdown

Please use visudo for changing /etc/sudoers, as otherwise you can brick sudo…

(Nakh Home) #6

always read the last line :slight_smile: ii messed up

sudo blocked

(Udo Hartmann) #7

Should be no real problem. Please try


to login as root (after logging in as the normal user.)


after logging in as root, you can destroy your os in seconds.

Then repair the damage, by using


again. (You have to know what is the fault in /etc/sudoers to correct it. visudo will only prevent you from saving a damaged configuration)

(Nakh Home) #8

Thanks Udo

I have made a fresh install

Will retry without forgetting visudo:)

(Udo Hartmann) #9

Well, even if your pi won’t start anymore (let’s say, because you bricked one of the essential files), there is no need to start from scratch. You could use another GNU/Linux system (life CD, vm, another computer…), then mount the filesystem (very easy, because it’s on a SDcard :slight_smile: ) and repair the damage. Of course, you have to know, what you’ve damaged… :wink:

(Nakh Home) #10

Thanks Udo, i have made a backup of my SD.

(Curtis) #11

I’m really struggling to get this working :frowning: Please can someone nudge me in the right direction?

I’ve read the docs regarding the fact that the user “openhab” needs to be able to run the shutdown comamand in order for this to work. I feel this is where I’m going wrong. As the following does not work:

sudo -u openhab reboot

Here are the steps I have taken which have been unsuccessful:

  1. Log into my Pi and add the following to /etc/sudoers.d/common_users
openhab ALL = NOPASSWD: /sbin/reboot, /sbin/shutdown
  1. Add the Exec Binding Add RegEx Transformation Service

  2. Added Thing in Paper UI:



Switch Reboot "Reboot" { channel="exec:command:6dba3517:run" }


Switch item=Reboot mappings=[OFF="Off", ON="On"]

Any help would be much appreciated thanks.

(Udo Hartmann) #12

Yes, this will not work, as it’s incorrect. :slight_smile:
You have to use

sudo -u openhab sudo reboot

This is: "execute the following commmand as user openhab: sudo reboot"
When using sudo the first time (as user openhab), this will result in a message, to respect the privacy of all users. as an alternative, you can change to a bash with login openhab:

sudo su openhab -s /bin/bash

As result you are logged in as user openhab, even if there is no password set and no login shell defined.
No you can perform

sudo reboot

and you will get the message about privacy:

sudo ls

Wir gehen davon aus, dass der lokale Systemadministrator Ihnen die
Regeln erklärt hat.  Normalerweise läuft es auf drei Regeln hinaus:

    #1) Respektieren Sie die Privatsphäre anderer.
    #2) Denken Sie nach, bevor Sie tippen.
    #3) Mit großer Macht kommt große Verantwortung.

[sudo] Passwort für openhab:

(sorry, only localized message here…)

Be aware that the path to reboot is /sbin/reboot, not /bin/reboot

(Curtis) #13

Woohoo, it is working now. I noticed as I left it at a 60 second interval by mistake and my Pi kept restarting. :grin:

Thanks for your help :+1:

(Francois Smit) #14

Thanks for this. Working like a charm, sortoff. Problem is that after I used it, it keeps on shutting down.

From what I could see it seems the switch is initialized from Null to On thus triggering the script again.

I’ve added autoupdate=“false” to the switch item but this doesn’t seem to have helped.

Will initializing the switch within the startup rules prevent this?

(Udo Hartmann) #15

Did you persist the item?

(Francois Smit) #16

I haven’t added anything in a persistence file.

Added the entry in things, items and sitemap as per your suggestion. Cut and pasted them. Just changed the bin to sbin

(Udo Hartmann) #17

Are you using exec1 or exec2?

(Francois Smit) #18

Looks like

binding-exec - 2.4.0
Exec Binding
This is the binding to execute arbitrary shell commands
Author: Karel Goderis