A sdcard friendly config on Raspberry
Posted: Thursday 16 February 2017 19:14
Domoticz tends to write often on disk. When disk is a sdcard it is better to avoid to write too often. The idea is to mount /tmp in ramdisk and have Domoticz to work in /tmp
Config: Raspberry Pi 3 with Raspbian Jessie.
I start Domoticz with systemd. File is /etc/systemd/system/domoticz.service :
Bash script /usr/local/bin/set-domoticz gets a valid DB at start and save DB to disk at stop :
Domoticz saves the on disk every hour. It's OK for me if I lost an hour of measure.
File heartbeat is for watchdog. For watchdog to monitor Domoticz, put in an lua script _time_XXX.lua :
and in /etc/watchdog.conf :
Watchdog can be dangerous: if things go wrong, Raspberry can reboot continuously. To avoid continuous reboot, I add to rc.local :
Config: Raspberry Pi 3 with Raspbian Jessie.
I start Domoticz with systemd. File is /etc/systemd/system/domoticz.service :
Code: Select all
[Unit]
Description=Domoticz home automation
After=network.target
[Service]
ExecStartPre=/usr/local/bin/set-domoticz start
ExecStart=/opt/domoticz/domoticz -www 81 -sslwww 0 -dbase /tmp/domoticz.db
ExecStopPost=/usr/local/bin/set-domoticz stop
Restart=always
[Install]
WantedBy=multi-user.target
Code: Select all
#! /bin/bash
# path to adapt
domoticzdir="/opt/domoticz" # where lives Domoticz
db="/tmp/domoticz.db" # BD path in a tmpfs filesystem
ozwlog="/tmp/OZW_Log.txt" # OpenZwave log path in a tmpfs filesystem
heartbeat="/tmp/domoticz.heartbeat" # heartbeat file for watchdog
########################################################################
bckbase="$domoticzdir/backups/hourly/backup-hour-"
checkdb() {
if [ "$(sqlite3 -batch -bail -cmd 'pragma integrity_check;' -cmd '.quit' $1 2> /dev/null)" == ok ]; then
rm -f $1-shm $1-wal
return 0
fi
return 1
}
case "$1" in
start)
touch $heartbeat
rm -f $domoticzdir/Config/OZW_Log.txt
ln -s $ozwlog $domoticzdir/Config/
[ -e $db -a checkdb $b ] && exit 0 # DB exists and is OK
# looking for a working DB, newer first
for f in $(ls -t $bckbase*.db); do
if checkdb $f; then
cp -a $f $db
exit 0
fi
done
;;
stop)
# save DB if it is OK
[ -f $db ] && checkdb $db && cp -a $db $bckbase$(date "+%H").db || exit 1
;;
esac
exit 0
File heartbeat is for watchdog. For watchdog to monitor Domoticz, put in an lua script _time_XXX.lua :
Code: Select all
-- pour le watchdog
file = io.open("/tmp/domoticz.heartbeat", "w")
file:close()
Code: Select all
file = /tmp/domoticz.heartbeat
change = 300
Code: Select all
# BdA added for the watchdog
lbf="/root/lastboot"
actuel=$(date +%s)
if [ -f "$lbf" ]; then
last=$(cat "$lbf")
if (( actuel - last < 120 )); then
systemctl stop watchdog
fi
fi
echo $actuel > "$lbf"
date | mailx -s "Raspberry $(hostname) boot" [email protected]