Automatically restart Domoticz
The raspberry pi is a very stable mini-computer that runs on Linux. It is also ideal to domoticz to run. However, it sometimes happens that Domoticz (for whatever reason) crashes. As a result, there is a risk of data loss or certain commands not being executed.
To avoid this problem you can run a bash script in the background which checks if Domoticz is still online. As soon as this script detects that Domoticz is offline, it will be stopped and then restarted.
What do you need for this?
- raspberry pi;
- domoticz;
- jq.
Let's assume that Domoticz is already installed on the Raspberry Pi and that everything is running fine.
Open Nano or another text editor on the Raspberry Pi and copy and paste the script below. Fill for:
domoticz_ip
the (internal) ip address of your Raspberry Pi.domoticz_port
into the port on which your Domoticz runs.device_id
the idx of a switch.
#!/bin/bash domoticz_ip="127.0.0.1" domoticz_port="8080" device_id="1" DOMOTICZ=`curl -s --connect-timeout 2 --max-time 5 "http://"$domoticz_ip": "$domoticz_port"/json.htm?type=devices&rid="$device_id` STATUS=`echo $DOMOTICZ | jq -r '.status'` if [ "$STATUS" = "OK" ] ; then exit else sleep 5 DOMOTICZ=`curl -s --connect-timeout 2 --max-time 5 "http://"$domoticz_ip":"$domoticz_port"/json.htm?type=devices&rid="$device_id` STATUS2=` echo $DOMOTICZ | jq -r '.status'` if [ "$STATUS2" = "OK" ] ; then exit else sleep 5 DOMOTICZ=`curl -s --connect-timeout 2 --max-time 5 "http://"$domoticz_ip":"$domoticz_port"/json.htm?type=devices&rid="$device_id` STATUS3=` echo $DOMOTICZ | jq -r '.status'` if [ "$STATUS3" = "OK" ] ; then exit else echo "domoticz offline... rebooting..." sudo service domoticz.sh stop sleep 8 sudo kill $(sudo netstat -anp | awk '/LISTEN/{if($4 ~ ":"$domoticz_port"$") { gsub("/.*","",$7); print $7; exit } }') sleep 8 echo "domoticz stopped, now starting up..." sudo service domoticz.sh start fi fi fi
Save the bash script as domoticz_state_checker.sh
in the map ~/domoticz/scripts
.
Install jq
Because we get JSON data and want to filter it on the key(s) STATUS, STATUS2 or STATUS3 and want to check the value of this, we need a JSON processor.
The jq is a command-line based JSON processor that allows to transform, filter, segment, map, or perform other operations on JSON data.
Connect to your Raspberry Pi via SSH or enter the following commands directly in the terminal:
sudo apt update sudo apt install -y jq
After the installation procedure has been completed, we can perform a test whether jq is working correctly.
echo '{"employees": {"employee": [{"id": "1","firstName": "Tom","lastName": "Cruise"},{"id": "2","firstName ": "Maria","lastName": "Sharapova"},{"id": "3","firstName": "Robert","lastName": "Downey Jr."}]}}' > test.json
The jq program has several filters to manipulate JSON data. The simplest filter is the dot .
. This filter leaves the input unchanged and prints the output in JSON format.
Enter the following command and the . filter returns the output in JSON format.
jq'.' test.json
{ "employees": { "employee": [ { "id": "1", "firstName": "Tom", "lastName": "Cruise" }, { "id": "2", "firstName": "Maria", "lastName": "Sharapova" }, { "id": "3", "firstName": "Robert", "lastName": "Downey Jr." } ] } }
Testing the script
Go to the folder ~/domoticz/scripts
and type in the command line the following command:
sh domoticz_state_checker.sh
If no messages are displayed in the command line, it seems that the script is working correctly. If we now temporarily stop Domoticz and run the script again, we will receive the following messages:
domoticz offline... rebooting... Usage: kill [options] [...] Options: [...] send signal to every listed - , -s, --signal specify the to be sent -l, --list=[ ] list all signal names, or convert one to a name -L, --table list all signal names in a nice table -h, --help display this help and exit -V, --version output version information and exit For more details see kill(1). domoticz stopped, now starting up...
Run the script automatically
We want this script to run automatically periodically. This is possible using a cron job
. A Cron is a task scheduler within Unix systems for performing tasks on a fixed date or time. To perform such a task we can enter this task in a cron table. To open/edit the cron table we run the following command in the command line:
crontab -e
You will now see the following in your editor:
# Edit this file to introduce tasks to be run by cron. # # Each task to run has to be defined through a single line # indicating with different fields when the task will be run # and what command to run for the task # # To define the time you can provide concrete values for # minute (m ), hour (h), day of month (dom), month (mon), # and day of week (dow) or use '*' in these fields (for 'any'). # # Notice that tasks will be started based on the cron's system # daemon's notion of time and timezones. # # Output of the crontab jobs (including errors) is sent through # email to the user the crontab file belongs to (unless redirected). # # For example, you can run a backup of all your user accounts # at 5 am every week with: # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/ # # For more information see the manual pages of crontab(5) and cron(8) # # mh dom mon dow command
Add the line below at the very bottom and save the file.
*/1 * * * * /domoticz/scripts/domoticz_state_checker.sh
Now the script will run every minute.
How the script works
In the above script, the JSON response is retrieved from the chosen device. If the key STATUS, STATUS2 or STATUS3 returns the value “OK”, Domoticz is online and the script is terminated.
If the condition is not met, then Domoticz is not available and Domoticz will be stopped with the command sudo service domoticz.sh stop
.
Then the command kill
in combination with other parameters the Domoticz process terminates.
At the end of the script, the command sudo service domoticz.sh start
Domoticz restarted.
Dit delen:
- Klik om te delen met Twitter (Wordt in een nieuw venster geopend)
- Klik om te delen op Facebook (Wordt in een nieuw venster geopend)
- Meer
- Klik om dit te e-mailen naar een vriend (Wordt in een nieuw venster geopend)
- Klik om op LinkedIn te delen (Wordt in een nieuw venster geopend)
- Klik om te delen met Reddit (Wordt in een nieuw venster geopend)
- Klik om op Tumblr te delen (Wordt in een nieuw venster geopend)
- Klik om te delen op Telegram (Wordt in een nieuw venster geopend)
- Klik om te delen op WhatsApp (Wordt in een nieuw venster geopend)
Hi Bjorn, I've been using Monit to check and restart all my processes on my Dietpi box.
It will give you optional emails to remind you something nasty happened. Seems to be pretty effective.
Hi,
Hi Bjorn,
Whatever device_id you enter in the script, 1 or 2 or 99998888 the result of jq is always OK
So for STATUS, STATUS2 and STATUS3
Check it out!
If you bring Domoticz down, the result will of course be a reboot.
Requesting the status of the Domoticz Instance 1x already gives sufficient results.
This by: /json.htm?type=command¶m=getversion
Thanks for this checker setup.!!
Frits