Script to restart Domoticz if it crashes Topic is solved
Moderator: leecollings
- Egregius
- Posts: 2589
- Joined: Thursday 09 April 2015 12:19
- Target OS: Linux
- Domoticz version: v2024.7
- Location: Beitem, BE
- Contact:
Re: Script to restart Domoticz if it crashes
The script starts wit the /bin/bash shabang, no need to specify bash. Just execute it.
-
- Posts: 21
- Joined: Sunday 19 March 2017 22:06
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: Script to restart Domoticz if it crashes
Hi, i'm try to run this code on OrangePI but i get error below:
Error output
Code: Select all
#!/bin/bash
DomoticzState=`sudo service domoticz.sh status`
if [[ $DomoticzState == *"active (running)"* ]]
then
echo 'Domoticz is running. Nothing to do.'
elif [[ $DomoticzState == *"inactive (dead)"* ]]
then
echo 'Domoticz is not running. Restarting Domoticz...'
sudo service domoticz.sh restart
echo 'Domoticz restarted.'
fi
Any suggestion?domoticz_state_checker.sh: 5: domoticz_state_checker.sh: [[: not found
domoticz_state_checker.sh: 8: domoticz_state_checker.sh: [[: not found
- Egregius
- Posts: 2589
- Joined: Thursday 09 April 2015 12:19
- Target OS: Linux
- Domoticz version: v2024.7
- Location: Beitem, BE
- Contact:
Re: Script to restart Domoticz if it crashes
Try the script provided earlier in this topic.
It's a better way than just querying the state of a service. A service can be running but dead.
It's a better way than just querying the state of a service. A service can be running but dead.
- michaldobrotka
- Posts: 50
- Joined: Sunday 01 November 2015 17:21
- Target OS: Raspberry Pi / ODroid
- Domoticz version: Beta
- Contact:
Re: Script to restart Domoticz if it crashes
Hi Guys,uc530 wrote:Hi!
Script without loop and crontab command works like charm.
Thanks again for help.
script and added all rights:Code: Select all
#!/bin/bash DomoticzState=`sudo service domoticz.sh status` if [[ $DomoticzState == *"active (running)"* ]] then echo 'Domoticz is running. Nothing to do.' elif [[ $DomoticzState == *"inactive (dead)"* ]] then echo 'Domoticz is not running. Restarting Domoticz...' sudo service domoticz.sh restart echo 'Domoticz restarted.' fi
crontab edit:With best regards UC530Code: Select all
crontab -e */1 * * * * /home/pi/domoticz/scripts/domoticz_state_checker.sh
I am trying to get running this script. Since Problems with Phyton started Months ago I cant get Domoticz running without crash for more than 2 days. I saved a script (using one quoted above), executed it and added it in crontab, and rc. local to auto startup. How can I check it if is it working (via ssh) ? (Sorry I am noob in Linux and cant find for me understandable answer )
Thanks
Domoticz on Raspi 3 and slave on ZeroW (running only with Broadlink plugin), Serial Mysensors GW on Arduino nano, Broadlink RM2, Rehau CO2 USB Stick, RF link 433 MHz, 2x Amazon Echo Dot (Controlicz), ESP2866 witk Espeasy, Sonoff
- Egregius
- Posts: 2589
- Joined: Thursday 09 April 2015 12:19
- Target OS: Linux
- Domoticz version: v2024.7
- Location: Beitem, BE
- Contact:
Re: Script to restart Domoticz if it crashes
Use the script in http://www.domoticz.com/forum/viewtopic ... 80#p137092 but only in cron, not as a service.
- EdwinK
- Posts: 1820
- Joined: Sunday 22 January 2017 21:46
- Target OS: Raspberry Pi / ODroid
- Domoticz version: BETA
- Location: Rhoon
- Contact:
Re: Script to restart Domoticz if it crashes
Thanks. Using the one mentioned in the above post.
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | Hue | Tuya | IKEA tradfri | Dashticz V3 on Lenovo Huawei Tablet | Conbee
-
- Posts: 200
- Joined: Thursday 06 October 2016 8:14
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 3.7243
- Contact:
Re: Script to restart Domoticz if it crashes
After handy execution of the script I have such errors
My script
Please help
Code: Select all
root@osmc:/home/domoticz# ./domoticz_state_checker.sh
./domoticz_state_checker.sh: line 3: jq: command not found
./domoticz_state_checker.sh: line 9: jq: command not found
./domoticz_state_checker.sh: line 15: jq: command not found
Code: Select all
#!/bin/bash
DOMOTICZ=`curl -s --connect-timeout 2 --max-time 5 "http://127.0.0.1:8078/json.htm?type=devices&rid=43"`
STATUS=`echo $DOMOTICZ | jq -r '.status'`
if [ "$STATUS" == "OK" ] ; then
exit
else
sleep 5
DOMOTICZ=`curl -s --connect-timeout 2 --max-time 5 "http://127.0.0.1:8078/json.htm?type=devices&rid=43"`
STATUS2=`echo $DOMOTICZ | jq -r '.status'`
if [ "$STATUS2" == "OK" ] ; then
exit
else
sleep 5
DOMOTICZ=`curl -s --connect-timeout 2 --max-time 5 "http://127.0.0.1:8078/json.htm?type=devices&rid=43"`
STATUS3=`echo $DOMOTICZ | jq -r '.status'`
if [ "$STATUS3" == "OK" ] ; then
exit
else
sudo service domoticz.sh stop
sleep 8
sudo kill $(sudo netstat -anp | awk '/ LISTEN / {if($4 ~ ":8078$") { gsub("/.*","",$7); print $7; exit } }')
sleep 8
sudo service domoticz.sh start
fi
fi
fi
-
- Posts: 200
- Joined: Thursday 06 October 2016 8:14
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 3.7243
- Contact:
Re: Script to restart Domoticz if it crashes
ThanksEgregius wrote:apt-get install jq
-
- Posts: 82
- Joined: Thursday 02 March 2017 20:28
- Target OS: Linux
- Domoticz version: V4.1174
- Location: Netherlands
- Contact:
Re: Script to restart Domoticz if it crashes
May be i missed it.. but why don't use this way :
https://www.domoticz.com/wiki/Monitoring_domoticz
Monit not a nice tool ?
https://www.domoticz.com/wiki/Monitoring_domoticz
Monit not a nice tool ?
Raspberry PI 3 - Opentherm Gateway + ESP8826 - EvoHome - ESPEasy CO2 + Fancontrol - RF-Link - zigbee2mqqt -
- Egregius
- Posts: 2589
- Joined: Thursday 09 April 2015 12:19
- Target OS: Linux
- Domoticz version: v2024.7
- Location: Beitem, BE
- Contact:
Re: Script to restart Domoticz if it crashes
Because a listening port isn't a good way to check if a service is running.
-
- Posts: 82
- Joined: Thursday 02 March 2017 20:28
- Target OS: Linux
- Domoticz version: V4.1174
- Location: Netherlands
- Contact:
Re: Script to restart Domoticz if it crashes
Read more now, and understand.
I restart the domoticz service every 24 hr (crontab) but still have that domoticz website is perfectly running but the logging (all devices) is after aprox 1 week stopped.
If i then restart domoticz again it will work agian aprox for 1 week.
Which is sligtly worse as without the crontab start/stop action.
Can we not make something in this way :
Make a " device " in Domoticz which we connect with a random generator (or simply a clock device).
If the random generator showes few times the same value, then restart domoticz.
I restart the domoticz service every 24 hr (crontab) but still have that domoticz website is perfectly running but the logging (all devices) is after aprox 1 week stopped.
If i then restart domoticz again it will work agian aprox for 1 week.
Which is sligtly worse as without the crontab start/stop action.
Can we not make something in this way :
Make a " device " in Domoticz which we connect with a random generator (or simply a clock device).
If the random generator showes few times the same value, then restart domoticz.
Raspberry PI 3 - Opentherm Gateway + ESP8826 - EvoHome - ESPEasy CO2 + Fancontrol - RF-Link - zigbee2mqqt -
- Egregius
- Posts: 2589
- Joined: Thursday 09 April 2015 12:19
- Target OS: Linux
- Domoticz version: v2024.7
- Location: Beitem, BE
- Contact:
Re: Script to restart Domoticz if it crashes
Are the lastupdate times still updated then? Otherwise you may do something with that.
-
- Posts: 82
- Joined: Thursday 02 March 2017 20:28
- Target OS: Linux
- Domoticz version: V4.1174
- Location: Netherlands
- Contact:
Re: Script to restart Domoticz if it crashes
In the log i don't see anything any more, but I know that for example, my ESP easy still data sends (via MQTT), and Evohome is also still running.
The data is only not logged/displayed in Domoticz, Domoticz keeps on showing the same value. (so get a flat line), bu the webpage is still running perfectly.
After sudo stop domoticz, sudo start domoticz, everything works gain as it should.
The data is only not logged/displayed in Domoticz, Domoticz keeps on showing the same value. (so get a flat line), bu the webpage is still running perfectly.
After sudo stop domoticz, sudo start domoticz, everything works gain as it should.
Raspberry PI 3 - Opentherm Gateway + ESP8826 - EvoHome - ESPEasy CO2 + Fancontrol - RF-Link - zigbee2mqqt -
- Egregius
- Posts: 2589
- Joined: Thursday 09 April 2015 12:19
- Target OS: Linux
- Domoticz version: v2024.7
- Location: Beitem, BE
- Contact:
Re: Script to restart Domoticz if it crashes
In that case I would get the json of the used devices. Loop thru the lastupdate times and set a variable with the highest value. Then, if variable is more than x minutes ago restart.
-
- Posts: 82
- Joined: Thursday 02 March 2017 20:28
- Target OS: Linux
- Domoticz version: V4.1174
- Location: Netherlands
- Contact:
Re: Script to restart Domoticz if it crashes
Will see if i can manage that. Will use the scripts in this topic as reference.
Thanks for your feedback
.
Thanks for your feedback

Raspberry PI 3 - Opentherm Gateway + ESP8826 - EvoHome - ESPEasy CO2 + Fancontrol - RF-Link - zigbee2mqqt -
- Egregius
- Posts: 2589
- Joined: Thursday 09 April 2015 12:19
- Target OS: Linux
- Domoticz version: v2024.7
- Location: Beitem, BE
- Contact:
Re: Script to restart Domoticz if it crashes
In PHP very simple:
Run the script from cron, for example:
Code: Select all
<?php
$domoticz=json_decode(file_get_contents('http://127.0.0.1:8080/json.htm?type=devices&used=true'),true);
if($domoticz){
$last=0;
$now=time();
foreach($domoticz['result'] as $dom){
if(strtotime($dom['LastUpdate'])>$last)$last=strtotime($dom['LastUpdate']);
}
$last=$now-$last;
echo 'Last update was '.$last.' seconds ago.'.PHP_EOL;
if($last>300){
shell_exec('sudo service domoticz restart');
telegram('Domoticz service restarted, last update was '.$last.' seconds ago.');
}
}
Code: Select all
* * * * * /usr/bin/php /home/pi/checklastupdate.php
-
- Posts: 82
- Joined: Thursday 02 March 2017 20:28
- Target OS: Linux
- Domoticz version: V4.1174
- Location: Netherlands
- Contact:
Re: Script to restart Domoticz if it crashes
Thanks !
Will try and let you know.
Will try and let you know.
Raspberry PI 3 - Opentherm Gateway + ESP8826 - EvoHome - ESPEasy CO2 + Fancontrol - RF-Link - zigbee2mqqt -
-
- Posts: 67
- Joined: Sunday 16 February 2014 17:40
- Target OS: Raspberry Pi / ODroid
- Domoticz version: Beta
- Location: NL
- Contact:
Re: Script to restart Domoticz if it crashes
Hi because sometimes my Domoticz crashes I adapt one of the script and changed it a little bit.
However the script is not reliable, ones a day the script says Domoticz is down/crashed but is still running.
I commented out the service start/restart at the moment.
Can it be a timing issue?
However the script is not reliable, ones a day the script says Domoticz is down/crashed but is still running.
I commented out the service start/restart at the moment.
Code: Select all
#!/bin/bash
#
dt=$(date '+%d/%m/%Y %H:%M:%S')
#
LOG=/RAMDISK/domoticz_state_crash.log
#
DOMOTICZ=`curl -s --connect-timeout 2 --max-time 5 "http://127.0.0.1:8080/json.htm?type=devices&rid=1"`
STATUS=`echo $DOMOTICZ | jq -r '.status'`
if [ "$STATUS" == "OK" ] ; then
echo "$dt: OK" | tee -a $LOG
exit
else
sleep 5
DOMOTICZ=`curl -s --connect-timeout 2 --max-time 5 "http://127.0.0.1:8080/json.htm?type=devices&rid=1"`
STATUS2=`echo $DOMOTICZ | jq -r '.status'`
if [ "$STATUS2" == "OK" ] ; then
echo "$dt: STATUS2=$STATUS2" | tee -a $LOG
exit
else
sleep 5
#DOMOTICZ=`curl -s --connect-timeout 2 --max-time 5 "http://127.0.0.1:8080/json.htm?type=devices&rid=1"`
DOMOTICZ=`curl -s --connect-timeout 3 --max-time 10 "http://127.0.0.1:8080/json.htm?type=devices&rid=1"`
STATUS3=`echo $DOMOTICZ | jq -r '.status'`
if [ "$STATUS3" == "OK" ] ; then
echo "$dt: STATUS3=$STATUS3" | tee -a $LOG
exit
else
###sudo service domoticz.sh stop
sleep 8
###sudo kill $(sudo netstat -anp | awk '/ LISTEN / {if($4 ~ ":8080$") { gsub("/.*","",$7); print $7; exit } }')
sleep 8
###sudo service domoticz.sh start
#
echo "$dt: Domoticz was offline. Restarted Domoticz...." >> "$LOG"
#
/home/pi/pushover_shell.sh "`date '+%Y%m%d%H%M'`: Domoticz crashed (v3) but has been restarted !!!" "Crash report script v3"
#
fi
fi
fi
- Egregius
- Posts: 2589
- Joined: Thursday 09 April 2015 12:19
- Target OS: Linux
- Domoticz version: v2024.7
- Location: Beitem, BE
- Contact:
Re: Script to restart Domoticz if it crashes
If the script says that domoticz is crashed it means that domoticz didn't respond for 3 times. The 3 tries before restarting is there to prevent falsely restarting.
Must be that your system is sometimes to busy to respond.
Does it restarts always at the same time? Is there something in the log at that time?
Besides that I can only say that I use the script for a very long time on both Synology and Raspberry and it never restarts when not needed.
Must be that your system is sometimes to busy to respond.
Does it restarts always at the same time? Is there something in the log at that time?
Besides that I can only say that I use the script for a very long time on both Synology and Raspberry and it never restarts when not needed.
Who is online
Users browsing this forum: No registered users and 0 guests