Domoticz auto cleaning

Domoticz auto cleaning

When I started out with Domoticz I was very pleased. Smooth running, fast and very responsive. After some time though, I noticed that the webinterface was slow, some of the scripts slow etcetera. After some frustrating investigation I found it helps to restart Domoticz daily. I know, Linux, it should not be needed but it does the trick for me. What helps is that during this restart, between the shutdown and the restart, I clean the database and reload the daemons. I also move the backup files to my NAS and the same for the log file. That way I can keep the logs and backups for longer since the card for the Pi is not that large. I have been so happy with the result that I’ve decided to share it.

In the cron I have put a daily task to restart Domoticz. I have decided to do this at exactly midnight because of some counter scripts that I have running on Domoticz.

Open crontab:

sudo crontab -e

and enter this line:

0 0 * * * /home/pi/

The script does the following

  • stop Domoticz
  • clean the database
  • reload the daemons
  • save the log file
  • move the log files to the NAS
  • start a new log
  • move the backup files to the NAS
  • restart Domoticz

# Show date and time of restart (convenient when manually restarting)
echo $(date +%F_%H-%M-%S)
echo .

# Stop Domoticz
echo Stopping Domoticz
sudo service stop

# Clean up the database
echo Cleaning database
sudo php /home/pi/domoticz/scripts/my_own/cleanup_db.php

# Reload the daemons
echo Reloading daemons
sudo systemctl daemon-reload

# Save the log file
cp /home/pi/log/domoticz.log /home/pi/log/$(date +%F_%H-%M-%S).domoticz.log
chown pi:pi /home/pi/log/*

# Move log to the NAS and start a new one
mv /home/pi/log/2* /mnt/synology/log/
echo . > /home/pi/log/domoticz.log

# Move all backup files except that of today to the NAS
DAY=$(date +%d)
echo $DAY
for f in $FILES
  if [ $f != "/home/pi/domoticz/backups/daily/backup-day-$DAY-Domoticz.db" ]
      sudo mv $f /mnt/synology/backups/

# Restart Domoticz
echo Starting Domoticz
sudo service start



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s