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.
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.
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.
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.