Re: Script to restart Domoticz if it crashes
Posted: Friday 10 March 2017 8:59
I'll post my script when I am at home tonight or tomorrow
Open source Home Automation System
https://forum.domoticz.com/
Code: Select all
#!/bin/sh
# check ZWave
CONFIG=/home/pi/domoticz/scripts/bash/domoticz_state_checker.txt
dt=$(date '+%d/%m/%Y %H:%M:%S')
status=`curl -s -i -H "Accept: application/json" "http://xxx.xxx.xxx:xxxx/json.htm?type=devices&rid=1" | grep "status"| awk -F: '{print $2}'|sed 's/,//'| sed 's/\"//g'`
if [ $status ]
then
echo "Zwave is responsive!"
else
sudo service domoticz.sh stop
sleep 5
sudo service domoticz.sh start
echo echo "$dt, ZWave was offline. Restarted Domoticz...." >> "$CONFIG"
fi
Code: Select all
#!/bin/bash
DOMOTICZ=`curl -s --connect-timeout 2 --max-time 5 "http://127.0.0.1:8084/json.htm?type=devices&rid=1"`
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:8084/json.htm?type=devices&rid=1"`
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:8084/json.htm?type=devices&rid=1"`
STATUS3=`echo $DOMOTICZ | jq -r '.status'`
if [ "$STATUS3" == "OK" ] ; then
exit
else
curl -s --connect-timeout 2 --max-time 5 --data-urlencode "text=Domoticz Bad - Restarting" --data "silent=false" http://127.0.0.1/secure/telegram.php
NOW=$(date +"%Y-%m-%d_%H%M%S")
cp /tmp/domoticz.log /volume1/files/temp/domoticz-$NOW.txt
sudo /var/packages/domoticz/scripts/start-stop-status stop
sleep 8
sudo kill $(sudo netstat -anp | awk '/ LISTEN / {if($4 ~ ":8084$") { gsub("/.*","",$7); print $7; exit } }')
sleep 8
sudo /var/packages/domoticz/scripts/start-stop-status start
fi
fi
fi
Code: Select all
#!/bin/bash
dt=$(date '+%d/%m/%Y %H:%M:%S')
CONFIG=/home/pi/domoticz/scripts/bash/domoticz_state_checker.txt
STATUS=`curl -s --connect-timeout 2 --max-time 5 "Accept: application/json" "http://192.168.1.109:8080/json.htm?type=devices&rid=1" | grep "status"| awk -F: '{print $2}'|sed 's/,//'| sed 's/\"//g'`
if [ $STATUS ] ; then
exit
else
sleep 5
STATUS2=`curl -s --connect-timeout 2 --max-time 5 "Accept: application/json" "http://192.168.1.109:8080/json.htm?type=devices&rid=1" | grep "status"| awk -F: '{print $2}'|sed 's/,//'| sed 's/\"//g'`
if [ $STATUS2] ; then
exit
else
sleep 5
STATUS3=`curl -s --connect-timeout 2 --max-time 5 "Accept: application/json" "http://192.168.1.109:8080/json.htm?type=devices&rid=1" | grep "status"| awk -F: '{print $2}'|sed 's/,//'| sed 's/\"//g'`
if [ $STATUS3 ] ; then
exit
else
NOW=$(date +"%Y-%m-%d_%H%M%S")
sudo service domoticz.sh stop
sleep 8
sudo killall domoticz
sleep 8
sudo service domoticz.sh start
echo "$dt, Domoticz was offline. Restarted Domoticz...." >> "$CONFIG"
fi
fi
fi
Trying this, after my version of Domoticz had already crashed. This is the output.Egregius wrote:This is my version:
If the status is OK, nothing happens. If not 2 retries are done.
If all 3 are NOK the logfile is copied (otherwise cleared because tmpfs), the service is stopped, then kill whatever is running on port 8084 and start it again.
the kill is added because I once encountered that service stop didn't work because domoticz was really crashed but still running and blocking the port.
Keep in mind that this is for Syno, should be big thing to change it to RPi.
Code: Select all
cp: cannot stat ‘/tmp/domoticz.log’: No such file or directory
Domoticz is not running
kill: not enough arguments
Starting Domoticz ...
insmod: ERROR: could not insert module /lib/modules/usbserial.ko: File exists
insmod: ERROR: could not insert module /lib/modules/ftdi_sio.ko: File exists
insmod: ERROR: could not insert module /usr/local/domoticz/modules/cp210x.ko: File exists
insmod: ERROR: could not insert module /usr/local/domoticz/modules/pl2303.ko: File exists
insmod: ERROR: could not insert module /usr/local/domoticz/modules/ch341.ko: File exists
insmod: ERROR: could not insert module /usr/local/domoticz/modules/ti_usb_3410_5052.ko: File exists
ln: failed to create symbolic link ‘/lib/udev/rules.d/60-synocommunity.domoticz.rules’: File exists
Code: Select all
status=`curl -s --form-string "token=pushoverapitoken" --form-string "user=pushoverusertoken" --form-string "message=Domoticz is opnieuw gestart" https://api.pushover.net/1/messages.json`
Installed. Hope it's working.Egregius wrote:This is my version:
...
If the status is OK, nothing happens. If not 2 retries are done.
If all 3 are NOK the logfile is copied (otherwise cleared because tmpfs), the service is stopped, then kill whatever is running on port 8084 and start it again.
the kill is added because I once encountered that service stop didn't work because domoticz was really crashed but still running and blocking the port.
Keep in mind that this is for Syno, should be big thing to change it to RPi.
Hi G3rard,G3rard wrote:Thanks @K3rryBlue for sharing the script. Works fine on my Ubuntu Server with some small changes.
Here it is. Just change the folders and Domoticz URL and port to yours and it should work.Caribou wrote: Hi G3rard,
Would you mind to share your version including these small changes please?
Code: Select all
#!/bin/bash
dt=$(date '+%d/%m/%Y %H:%M:%S')
CONFIG=/home/gerard/scripts/domoticz/domoticz_state_checker.txt
STATUS=`curl -s --connect-timeout 2 --max-time 5 "Accept: application/json" "http://192.168.1.157:8084/json.htm?type=devices&rid=1" | grep "status"| awk -F: '{print $2}'|sed 's/,//'| sed 's/\"//g'`
if [ $STATUS ] ; then
echo "Domoticz online"
echo "$STATUS"
exit
else
sleep 5
STATUS2=`curl -s --connect-timeout 2 --max-time 5 "Accept: application/json" "http://192.168.1.157:8084/json.htm?type=devices&rid=1" | grep "status"| awk -F: '{print $2}'|sed 's/,//'| sed 's/\"//g'`
if [ $STATUS2] ; then
exit
else
sleep 5
STATUS3=`curl -s --connect-timeout 2 --max-time 5 "Accept: application/json" "http://192.168.1.157:8084/json.htm?type=devices&rid=1" | grep "status"| awk -F: '{print $2}'|sed 's/,//'| sed 's/\"//g'`
if [ $STATUS3 ] ; then
exit
else
NOW=$(date +"%Y-%m-%d_%H%M%S")
cp /var/log/domoticz.log /home/gerard/scripts/domoticz/domoticz-$NOW.txt
echo "Domoticz offline"
#You can send a Pushover message here
sudo service domoticz.sh restart
echo "$NOW, Domoticz was offline. Restarted Domoticz...." >> "$CONFIG"
fi
fi
fi