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.