Shutting down openhab, from the openhab interface

Today my family had to restart the electricity.

At this moment we don’t have the option to shut down OpenHab properly before shutting down the electricity. (Except doing from a terminal window)

What would be the best way to do a proper shutdown of OpenHab, from the user interface?


PS (I’m asking as the improper shutdown left logs that needed to be cleared before OpenHab booted properly)

Unless you have an item and rule setup, there is no way to do this through one of the UIs. You’ll need to get into Karaf or a shell to shut it down otherwise.

Set-up a virtual switch and in a rule use the executeCommandLine("sudo systemctl stop openhab2.service") for linux.

1 Like

Thanks Vincent I was looking for something like this.
If the stopping happens this healthy way, would I still need to clear the cache folders?


This post might help you.

In Linux there are issues with the “OpenHab” user not being able to call system commands, like systemctl and reboot, due to user restrictions.

There are lots of posts and topics on the subject.

Good luck

1 Like

You really need to look at shutting down the computer OH is running on, not just the openHAB service.

And in a Thread I’ve seen in the past this is more challenging than it first appears because when you issue the command to reboot or shutdown it kills all the running processes which includes OH which causes the shutdown to abort. Or something like that. I don’t actually remember the details.

So review the post Stuart posted for how to give the openhab user permission to run the shutdown command. Then use sudo shutdown -h +1 which will bring down the computer one minute after the command is issued. I’m hoping that scheduling the shutdown instead of using now will prevent the stopping of OH from canceling the shutdown.

You might consider a battery backup. One of those Li battery bricks people use to charge their phone is probably more than adequate to keep an RPi going for 30 minutes or so. If you are on something bigger it will obviously take an UPS.

If it is an RPi you might consider adding a button connected to the GPIOs. There are tons of tutorials. It’s one of the easier tools to use.

One tool I’ve found useful in a pinch is an ssh client on my phone. On Android there is JuiceSSH. I don’t know about iOS. In a pinch I’m able to connect to my OH server and issue commands on the command line from my phone. I have an OpenVPN set up so I can even do this from anywhere I have network access, not just from my LAN.

1 Like

I have an OpenVPN set up so I can even do this from anywhere I have network access, not just >from my LAN.

I want to avoid opening up my openhab to the outside world.

That is the point of OpenVPN. It isn’t open to the outside world. It provides an encrypted tunnel that only someone who has both a certificate and the password for the certificate can connect to. Once connected, that device will become part of your LAN (if configured in that way).

It’s the most secure way to access a system remotely.

I know what a VPN is.
I know it’s not open to the world, yet it’s a door, I prefer not to have.

So this invalidates EVERYTHING else I suggested?

About the only thing the OpenVPN is related to is being able to access ssh when not at home. It has nothing to do with 95% of my post.

That’s a great idea I might grab one would have too be decent the pi 3 b+ is power hungry but tbh I could do with a ups already for my other network equipment like my nas

I would also like too do that and add an external temp sensor that openhab can access I have posted about that before it’s beyond my skill for the moment

I never said anything about the rest.
I will try out the shutdown of the machine, once I’m able to shutdown Openhab.
1 step at the time.

When you shutdown the machine it shutsdown openHAB. If you are concerned about power failures, it is the machine you need to shutdown gracefully. The machine losing power is what can corrupt the SD card and file system. Shutting down OH separately isn’t the first step in the path to solving your problem. It is not relevant to your stated problem.

tl;dr, shutting down the machine also shuts down OH nicely and in an impending power failure it is the machine that needs to be shut down to prevent damage or data loss, not OH.

I know I have to have the machine shutdown gracefully. I never said I would not.

Shutting down openhab from a script is my first step.
Not because I need to shut openhab down first, it’s my first step because I need to be able to test if I can run a script from within openhab.

That is the hard part, what the script will run, might also be harder then I think, yet the fact that there are many post about running such a script, makes it my first step.
I understand it’s not be your first step.

I was able to run a script before that did a git pull, yet a stopping of openhab I’m not yet although both need sudo rights.


My main point is it is not a first step in actually solving your stated problem. Shutting down OH with systemctl does nothing to help you in a power outage situation. So why not spend that time to figure out how to run the systemctl command instead on running the shutdown command. You will have to do that eventually anyway, it takes exactly the same skills and learning, and you don’t have to waste time fighting working with systemctl when all that is throw away effort anyway. You don’t need it.

But if you are more comfortable running one system command versus another that’s fine. I was just trying to save you some uneeded effort and wasted time.

1 Like

I’m trying to tell you that it’s not wasted time as I first need to figure out the way to acces a script with correct acces. What’s inside the script is at that moment a detail.

The question not asked here is why would you want to do it that way? Why shutdown OH from OH? So your family can flick a switch?

otherwise @rlkoshak made a good suggestion (and for iOS I use Remoter VNC with ssh support). With autologin and a proper alias set it’s almost switch simplicity for your family.

but that all depends on your reasons for why…

With autologin and a proper alias set it’s almost switch simplicity for your family.
That only is true when
A) everyone has a phone
B) the same kind of phone

By adding a button to the website, it works for everyone, website users, ios and android users.
I can even link it to a physical button in my house, which would be even easier.

There are also many use cases for just shutting down Openhab, for example for upgrading from (another button) I’m not working on that, yet that would be a good use case too.
And for that one, the sever can’t be shut down.


ok so you basically want a user friendly way anyone of your family can shutdown the server and/or the OH service.

1 Like

I would suggest to make your HA system immune to electricity shutdown. If you are using raspberry pi or simmilar SBC i can recommend this: It is easy to install and works great.