Restart OpenHAB by itself via UI

Hi there, I was trying to implement a restart button for BasicUI using the Exec binding with a thing in file exec.things

Thing exec:command:RestartOpenHAB			"Restart OpenHAB"			[command="/usr/bin/openhab-cli stop && /usr/bin/openhab-cli start", interval=0, autorun=false]

And an item in exec.items

Switch Restart_OpenHAB                "Restart OpenHAB"                                <reboot>             { channel="exec:command:RestartOpenHAB:run" }

and tried to run it with a BasicUI entry like

	Switch item=Restart_OpenHAB

Looking into /var/log/openhab2/openhab.log, nothing appears to happen which is because

ti@Basti-Pi:~ $ sudo -u openhab openhab-cli stop

A systemd service configuration exists...
Use 'sudo /bin/systemctl stop openhab2.service' to stop an openHAB service

This option needs to run as root! (e.g. use 'sudo openhab-cli stop')

Is there any way to restart Openhab from the UI without superuser rights? I mean, there should be…

(Every two nights my Raspi gets backupped. After Backup, openHAB gets started by the backup script but a few days a month my bluetooth low energy devices by Xiaomi won’t get detected properly. A manual restart helps here.)

Checkout this topic. It does a restart, but you can modify that solution to do a stop instead.

Thank you for your answer. Though I think it’s not a clean solution as openHAB executing commands as user openhab should be able to restart itself without any superuser rights, I’ll implement this workaround.
Thanks :slight_smile:

This is incorrect. Starting and stopping services is inherently a command that is and should only be run by root. There is no way to give permission to start or stop a service to individual users on a service by service basis. Either a user can restart all services or no services.

While there’s ways to hack that, it would be really bad advice.
Keep OH running during the backup.
If you don’t stop it you’ll probably also no longer encounter your BT device detection problems.

I don’t want to openHAB to start or stop any system service, that’s the point. It’s an internal openHAB thing to re-initialize the devices. Or is there another way than re-starting openHAB?

Compare it with, say, /usr/sbin/nginx -s reload.

You can issue Karaf console commands to restart bundles (BT binding ?).
But as I said, just don’t stop OH so your devices cannot fail to get initialized on restart.

It’s a byte-wise backup process of the whole SD card. Therefore, I want to stop any services running on the device to circumvent as many inconsitencies as possible. I know backupping this way is not the correct way, but it’s way much more comfortable (especially for recovery). See

There is absolutely no need to stop OH for that.
I also occasionally use dd to duplicate the SD card while running off it.
And in case you use openHABian it comes with a backup solution (“Amanda”) which also can reliably backup running systems.

Are you sure about that? Even though I use persistence with influxdb? The backup takes three hours. I’m just afraid my database won’t be consistent in the backup. Thanks for the hint.

I never heard of anyone to have a problem with that.
And even if it didn’t work, what’s the point ? Worst thing is you loose your persistence data.
If you want to be on the safe side then you can additionally generate exports of your data from within influxdb so you have a checkpoint you can restore in case.

1 Like

For those who want to go down this route I have the following script that I dropped into /etc/cron.daily to automatically back up InfluxDB. You probably need to modify the third line of script if you are not running in Docker and you will want to replace the last line to move the file to your preferred backup location.

I do not backup my servers since they all configured using scripts (which are backed up) anyway so I only backup the data. If I run into trouble, I can rebuild the server from scratch and restore the data.



echo "Backing up the database"

docker exec -it influxdb influxd backup -portable -database openhab_db -host localhost:8088 /var/lib/influxdb/backups

echo "Packaging up the files"
file=/tmp/influxdb-$(date +%Y-%m-%d_%H%M).tar
echo $file
tar cvf $file /opt/influxdb/data/backups
rm -rf /opt/influxdb/data/backups/*

echo "Copying backup to fafnir"
sudo -u rich scp $file fafnir:/srv/dev-disk-by-label-backups/working-backup

For those who might be using MongoDB for OH persistence (or anything else) I’ve also got this script I wrote to backup my wife’s Nightscout database.


mongodump -d nightscout -u nightscout -p <password> -o /tmp/dump
file=/tmp/mongo-$(date +%Y-%m-%d_%H%M).tgz
tar cvfz $file /tmp/dump
rm -rf /tmp/dump

sudo -u rich scp $file fafnir:/srv/dev-disk-by-label-backups/working-backup

You will need to change the mogo database, user, and password parameters to match your specific database. And as with the above, you will want to change the last line to copy the backup to your location of choice.

If you configure email on your machine properly using something like ssmtp you can even get a nice little email when the cron job runs.

1 Like

Thank you very much. I’ll use it without docker :blush:

Hi i am kind of noob in linux
i am ruining ubuntu container on Proxomx(hypervisor)
and OH is ruining on root

two Qs…

  1. if i am running OH on root do i need to do suders file thing?(not sure how to do it… yet)
    2.i am doing it from UI , is there anything different?
  2. what kind of value i need the item to be?(Regex,Def)

i am doing below steps and i don’t see anything in logs but the value changes to on then null

Switch item=RestartOH mappings=[ON="Restart"]

23:13:54.383 [INFO ] [smarthome.event.ItemStateChangedEvent] - RestartOH changed from NULL to ON
23:13:54.388 [INFO ] [smarthome.event.ItemStateChangedEvent] - RestartOH changed from ON to OFF

root is a special user. sudo is a way for other users to execute one single command as root. If OH is running as root there is no reason to use sudo, the command will already be run as root.

But as a general rule, it’s considered really bad practice to run a service like OH as root.

yep i got it in other post but as i said i am new at linux and not sure how to switch user now…
anyway above method does not work for me :frowning: