Beginner's guide to an openHABian reboot/shutdown switch

My whole network and the server and my remote db runs of a backup system (ups) so network is there so even then it all runs. But some times i need to reboot the pi in the db. And its in the roof so jip looking for a easy way out

Regards
Allen

Do you need the reboot to happen automatically via a rule, or would you trigger it manually? Before I set this up, I just used an Android app called Raspcontroller to reboot my RPi. That might be the easiest solution.

If you really think you need to restart it (why at all? Pis restart when power is restored. And any proper SW such as the MQTT client should restore connectivity with the server when that becomes available again) I’d suggest to invert the mechanism:
Set up a cron job or similar on your remote Pi to have it check back with your local system at regular intervals (MQTT broker or API call or whatever to use as an indicator for a working connection) and if it can NOT reach your local system then instruct it to reboot itself.

I second that, exactly what I would do. a sort of watchdog on the pi that should restart itself…

Nice tutorial! I have implemented this idea trough accessing gpio and switching gpio to reboot and shutdown using python script run at system start-up/boot.
My concern for this tutorial is that during the execution, openhab, grafana, influxdb, etc. cannot be stop gracefully during reboot or shutdown. Especial for Openhab.service since you cannot stop openhab then initiate shutdown.

@rlkoshak Based on this discussion, should I update this tutorial to use sudo shutdown -r instead of sudo reboot?

If so, am I correct that I’ll need to modify sudoers.d to:

# Allow openhab user to execute shutdown and poweroff commands
openhab   ALL=(ALL) NOPASSWD: /sbin/shutdown, /sbin/poweroff

I also noted that Buster now uses systemctl reboot. So in that case, is it:

# Allow openhab user to execute systemctl command
openhab   ALL=(ALL) NOPASSWD: /sbin/systemctl

And then for the commands:

sudo systemctl reboot
sudo systemctl poweroff

Or is sudo no longer necessary for Buster due to systemctl being available to all users?

Probably. Though there is some doubt as to whether that is an issue in Buster or not. But since it is an issue in older raspbians the tutorial could use an update. Apparently on Buster, both reboot and shutdow -r actually alias to systemctl reboot so all three are the same.

You still need to be root to do some operations using systemctl.

For the sake of beginners, would it be better to just go with this?

# Allow openhab user to execute shutdown, poweroff, and systemctl commands
openhab   ALL=(ALL) NOPASSWD: /sbin/shutdown, /sbin/poweroff, /sbin/systemctl

Or will that cause issues due to the shutdown and poweroff commands not being available in Buster?

The commands are there in Buster. They are just mapped to systemcl. But even so, I don’t think there would be a problem with sudoers if you give permission on a command that doesn’t exist.

Okay, I’ll make a note that this should work for both Jessie and Buster, but hasn’t been tested. Thanks!

I am trying to integrate

sudo $OPENHAB_RUNTIME/bin/backup

but since im pretty new to Openhab and still learning, i cant find a good startingpoint to do that.

Which rights do i have to add? Would

/sbin/systemctl

work?

And for the rule:

executeCommandLine("sudo@@$OPENHAB_RUNTIME/bin/backup")

I can’t help you with your specific question, but I’m curious as to what you’re trying to accomplish. It sounds like you’re trying to create a rule to run OH backups. If so, you should look at the Amanda backup tool.

Truth be told, I haven’t actually installed Amanda. For my purposes, it’s been fine to take an image of my SD card before I make major changes, and simply keep copies of my configuration files (items, rules, etc.) on Google Drive.

I keep images of my SD card aswell, but when trying new stuff i backup just for easy restore. After that i can try again. :slight_smile:
But sometime i forget to backup when turing off my computer. With this rule i could make a quick backup and copy it later on.

Thanks for perfect manual!

Please tell, how to stop OH before rebooting or/and to clear cache and reboot.

I mean command like this

sudo service openhab2 stop && sudo rm -rf /var/lib/openhab2/cache/* && sudo rm -rf /var/lib/openhab2/tmp/* && sudo reboot

Is there a way to execute it?

1 Like

@rpwong, thanks for your straigthforward guide.
I’m on the latest openHabian with Raspbian Buster and having trouble getting this to work.

Using the following command to create/change my sudoers file:
sudo visudo -f /etc/sudoers.d/openhab_userrights

Which in turn looks like this:

#Allow openhab user to execute systemctl command
openhab   ALL=(ALL) NOPASSWD: /sbin/systemctl

When I try to test reboot from the SSH console it is asking me for a password, which according to my understanding should not be happening as NOPASSWD is set in the sudoers file.

[14:18:27] openhabian@openhab:~$ sudo -u openhab /bin/bash
[14:21:36] openhab@openhab:/home/openhabian$ systemctl reboot
User openhabian is logged in on sshd.
Please retry operation after closing inhibitors and logging out other users.
Alternatively, ignore inhibitors and users with 'systemctl reboot -i'.
[14:21:45] openhab@openhab:/home/openhabian$ systemctl reboot -i
==== AUTHENTICATING FOR org.freedesktop.login1.set-wall-message ===
Authentication is required to set a wall message
Authenticating as: root
Password:
[14:22:50] openhab@openhab:/home/openhabian$ sudo systemctl reboot
.
.
.
[sudo] password for openhab:
Sorry, try again.
[sudo] password for openhab:
sudo: 1 incorrect password attempt
[14:23:21] openhab@openhab:/home/openhabian$

The default password for the openhab user (habopen) seems incorrect, but I am weary of changing it to avoid breaking other things.

When triggered over the sitemap the rule is running, but nothing is happening. My assumption is that this is due to authentication issues.

2019-12-15 13:46:43.086 [INFO ] [e.smarthome.model.script.Flag_System] - Rebooting Raspberry Pi
2019-12-15 13:46:43.116 [INFO ] [lipse.smarthome.io.net.exec.ExecUtil] - executed commandLine '[sudo, systemctl, reboot]'

I tried the rule also with a (space) instead of the @@ characters (as the commas from the log made me suspicious), but same result.

I am aware this roadblock is likely due to my limited Linux knowledge, but any help or feedback on this would be very much appreciated.

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.