Postpone OH startup until your external DB is online

In my setup I use a MariaDB server, running in another VM. When I restart my VM server OH sometimes loads faster then my DB and I get persistence errors. Only solution is to restart OH after my DB has fully started.

This simple solution let’s OH wait until my DB is available.

Create a file, for example ismysqlonline.sh:

#!/bin/bash
while ! echo 2>/dev/null > /dev/tcp/YOUR_DB_IP/YOUR_DB_PORT; do
  echo "waiting for MySQL/MariaDB to come online..."
  sleep 5s
done
  echo "MySQL/MariaDB is running."
exit 0

Edit /lib/systemd/system/openhab.service and add the following line before ExecStart:

ExecStartPre=/home/openhab3/ismysqlonline.sh (Modify to suit your location and filename)

Reload systemd: sudo systemctl daemon-reload

Resulting in:

Mar 12 17:26:40 openhab3 systemd[1]: Starting openHAB - empowering the smart home...
Mar 12 17:26:40 openhab3 ismysqlonline.sh[8666]: waiting for MySQL/MariaDB to come online...
Mar 12 17:26:45 openhab3 ismysqlonline.sh[8666]: waiting for MySQL/MariaDB to come online...
Mar 12 17:26:50 openhab3 ismysqlonline.sh[8666]: waiting for MySQL/MariaDB to come online...
Mar 12 17:26:55 openhab3 ismysqlonline.sh[8666]: waiting for MySQL/MariaDB to come online...
Mar 12 17:27:00 openhab3 ismysqlonline.sh[8666]: waiting for MySQL/MariaDB to come online...
Mar 12 17:27:05 openhab3 ismysqlonline.sh[8666]: waiting for MySQL/MariaDB to come online...
Mar 12 17:27:10 openhab3 ismysqlonline.sh[8666]: waiting for MySQL/MariaDB to come online...
Mar 12 17:27:15 openhab3 ismysqlonline.sh[8666]: MySQL/MariaDB is running.
Mar 12 17:27:15 openhab3 systemd[1]: Started openHAB - empowering the smart home.
3 Likes

Thanks!

Maybe just to add, depending on your hypervisor you can also define a start up order.
I run PSQL as my db on Proxmox and it always starts earlier than OH due to the start up order index.

I’m using LXC with these parameters:

boot.autostart: “true”
boot.autostart.delay: “10”
boot.autostart.priority: “10”

Of course different values for each container. Most of the time it works, sometimes not. That’s now fixed with above script.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.