Beginner's guide to an openHABian reboot/shutdown switch

The openhab user in the operating system doesn’t have a password. This is by design and is a Linux best practice. The password you are thinking of is for the Karaf Console user which has nothing to do with the operating system.

Beyond that, all I can offer is that it seems the sudoers file you created is not being loaded and applied for some reason. The NOPASSWD means it should not all for a password for that command.

Please try to login via su:

sudo su - openhab -s /bin/bash

tryout another sudo command like

openhab@openhab:~$ sudo ls -l

which should result in something like

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] password for openhab:

then escape and retry (now only the question for password should emerge). escape again.
now tryout systemctl reboot

Did you ever get this figured out? Sorry, I’ve been away from the forums for a long time, and I’m just getting caught up.

Hi @rpwong , sorry for the late reply and thanks for the interest. I basically gave up and started working on other topics in the meanwhile. When I pick it up again I’ll reactivate the thread.

Restarting Openhab via a rule works fine for me, based on the info in this thread.
Is there a way to also issue caraf commands in a similar way? I like to be able to restart a binding using a rule and a switch item via the Openhab app.

Here’s a discussion about restarting bindings. I’ve been meaning to implement this, but haven’t gotten around to it.

Hi @rpwong

I went down a similar path as yourself figuring out how to get a reboot+shutdown to work, though I used buttons on the sitemap (in a sub frame of the site map). I used the visibility option on the sitemap to make a Yes/No dialogue appear/disappear (as necessary) to confirm the action for reboot or shutdown.

Items

String 		Restart_Shutdown 		"Restart/Shutdown"		
Switch		Restart_confirm			"Confirm Restart"			
Switch 		Shutdown_confirm		"Confirm Shutdown"

Sitemap

Switch item=Restart_Shutdown	mappings=["1"="Restart", "2"="Shutdown"]
Switch item=Restart_confirm		mappings=["ON"="Yes","OFF"="No"]	visibility=[Restart_Shutdown=="1"]
Switch item=Shutdown_confirm	mappings=["ON"="Yes","OFF"="No"]	visibility=[Restart_Shutdown=="2"]

Rules

//****************************
//** Restart OpenHab Server **
//****************************
rule "Restart OpenHab Server"
	when
		Item Restart_confirm received command
	then
		switch (receivedCommand) {
			case ON : { logInfo("Restart", *** Restart Server - Sent from UI ***", "\n") Restart_Shutdown.postUpdate(3) Restart_confirm.postUpdate(OFF) executeCommandLine("sudo@@reboot", 10000) }
			case OFF : { Restart_Shutdown.postUpdate("3") }	
		}
	end

//*****************************
//** Shutdown OpenHab Server **
//*****************************
rule "Shutdown OpenHab Server"
	when
		Item Shutdown_confirm received command
	then
	switch (receivedCommand) {
			case ON : { logInfo("Shutdown", *** Shutdown Server - Sent from UI ***", "\n") Restart_Shutdown.postUpdate(3) Shutdown_confirm.postUpdate(OFF) executeCommandLine("sudo@@shutdown", 10000) }
			case OFF : { Restart_Shutdown.postUpdate("3") }	
		}
	end

Thought it worth throwing this into this post.

A small code change is needed for OH3. Please click this LINK for details

6 Likes

I followed all the instructions on the guide, with the only exception that I’m using HABPanel:

  1. Made openhab a super user
  2. Created the permissions file with visudo
  3. Tested the permissions and they worked
  4. Created the Item
String Flag_System "System" <switch>
  1. Created a selector with the exact same options
  2. Set up the rules
rule "Reboot openHAB"
when
    Item Flag_System received command "Reboot"
then
    logInfo("Flag_System", "Manually rebooting openHAB")
    executeCommandLine("sudo@@reboot", 5000)
end

However, when I change from Active to Reboot or Shutdown on HABPanel, nothing happens. I set the Karaf Console to trace the rule runtime with:

 log:set TRACE org.eclipse.smarthome.model.rule.runtime

I realized that the shutdown/reboot rules are not being triggered when I give the instruction on HABPanel, but other rules are being normally triggered.

My system is Raspbian 9 on a Raspberry Pi 3 with OpenHAB 2.5.2-1

Any suggestions?

Please don’t let user openhab become super user.
Instead use sudo and allow only the commands you need to be used without a password.

I don’t know if this is correct for your version, but in question of exec there is a white list file to permit commands to be executed by exec binding. This option is brand new.

Move to openHABian, debug your system if you still have issues that you cannot resolve without restarting OH, then keep your system running 24/7 and forget about that reboot switch. It’s unnecessary and potentially harmful.

I totally agree and also don’t like giving super powers to low-level users. However, when you implement the system for non-technical users, you cannot tell them “log in to the server via SSH and restart it”. You need to provide a user-friendly option, otherwise they will simply go and pull the plug.
OpenHAB should provide such a friendly (and secure) option by default instead of making us look for workarounds.

I personally believe that systems (even highly available ones) should be rebooted periodically for performance reasons, but that’s not part of this discussion and you’re right about pointing out that (doing it this way) is potentially harmful.

I’ll take a look at this.

Well, yes. :slight_smile: But as there is no option for now…

  1. login to the GNU/Linux platform where openHAB is active.
  2. if not already installed, install sudo
  3. grant access for the openhab user by editing the sudoers file. Use visudo for this purpose.
  4. setup a line like this: openhab ALL (ALL) NOPASSWD /sbin/reboot
    at the correct context (search for root in the file)
  5. install exec and create the correct thing to use the script
  6. edit the whitelist to allow openHAB exec to execute sudo /sbin/reboot
  7. link an item to the thing and setup a Switch Widget to use the exec channel ‘run’

No need for root. Stay save, but give the user control.
But please be aware that maybe the UI is not reachable anymore, so there might be a better way to restart openHAB by building a script which can be executed remotely (just by a double click on the Desktop Icon :wink: maybe with a simple “Are you sure?” )

2 Likes

Definitely no.
Reboots are not needed and do more harm than good. A simple accessible reboot switch just makes the lazy part in everybody surface and quickly get in control instead of taking the painful route to really debug your problem. This is even more true for beginners. That’s not the OH way of doing things.
And that is why most of us (openHABian developers, experienced supporters) with discernment will not support users in getting there.

I also disagree with this. If performance degrades over time, that’s just sign of another uncaught bug (mem leak or some such) or bad software design and your time is better invested into looking after that.

@z9th3, did you ever solve this? I’m doing a fresh install on an RPi4 and have run into the same issues with the openhab user requesting a password. @Udo_Hartmann’s suggestion unfortunately isn’t working for me.

I’m suspecting that this guide might need to be rewritten to use the Exec binding, but I’m not sure if that will work either.

@rpwong sorry for the late reply, I have not worked on this issue anymore so I don’t have any good news to report. My need for automatic reboots has decreased because it seems my current system is more stable.

This was my key to success:

1 Like

Thanks @sihui! I’ll try it out this weekend. I rarely need to reboot these days, but the shutdown command is nice to have on occasion. Mostly, I’d like to update the guide so that it works for others.

for this I added following rule

rule “piStartup_systemstatus”

when System started or

Time cron "0/15 * * * * ?"

then

postUpdate(Flag_System, NULL)

end

Did you ever get this figured out? Sorry, I’ve been away from the forums for a long time, and I’m just getting caught up.

On my installation, it was not enough to use

openhab ALL=(ALL) NOPASSWD: /sbin/systemctl

For some reason I had to use

openhab ALL=(ALL) NOPASSWD: ALL

to allow a rule to call ‘sudo -S systemctl restart openhab2.service’. No idea why allowing only systemctl is not enough.

Hello, your example works great for a long time on OH2.5 … But on OH3 I’m having an error:

2021-01-27 13: 36: 19.614 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘system-1’ failed: An error occurred during the script execution: array element type mismatch in system

I would know how to help me solve it, I miss “restart” in the UI.
Thanks!