Page 3 of 10

Re: Script to restart Domoticz if it crashes

Posted: Friday 10 March 2017 8:59
by jannl
I'll post my script when I am at home tonight or tomorrow

Re: Script to restart Domoticz if it crashes

Posted: Friday 10 March 2017 10:37
by K3rryBlue
Not sure havnegata, since i moved the installation from the SD card to a USB drive, it runs very stable. I expect that when Domoticz hangs, there will no response and enters the ELSE of the statement.

I'll wait for the script of Jan. Just saw below remark it's not working..................

Re: Script to restart Domoticz if it crashes

Posted: Friday 10 March 2017 10:50
by uc530
Hi again!
Holy.....
Egregius and jannl are right. Today my Domoticz hanged and script for detecting if it is running is ok and runs perfectly. I also checked manually if domo is running and it is all OK, but the web page is not responding...

Re: Script to restart Domoticz if it crashes

Posted: Friday 10 March 2017 10:57
by K3rryBlue
Just remember that I already have one to check the status of zwave....
fill in your own ip, port and idx which you want to check. In my case its 1. the number of the RAZBERY2 board.

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

Re: Script to restart Domoticz if it crashes

Posted: Friday 10 March 2017 11:18
by jannl
Basically that is what my script does as well

Re: Script to restart Domoticz if it crashes

Posted: Friday 10 March 2017 12:03
by Egregius
This is my version:

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
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.

Re: Script to restart Domoticz if it crashes

Posted: Friday 10 March 2017 19:01
by K3rryBlue
Hi Egregius,

Seems promising, I will look into it and adapt it for the rPi.

Re: Script to restart Domoticz if it crashes

Posted: Friday 10 March 2017 21:06
by K3rryBlue
This one works for the pi, needed to make more changes then expected.

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

Re: Script to restart Domoticz if it crashes

Posted: Sunday 12 March 2017 0:59
by G3rard
Thanks @K3rryBlue for sharing the script. Works fine on my Ubuntu Server with some small changes.

Re: Script to restart Domoticz if it crashes

Posted: Wednesday 15 March 2017 10:15
by EdwinK
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.
Trying this, after my version of Domoticz had already crashed. This is the output.

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
I'm not much of a coder, so much of it means not much for me.

Re: Script to restart Domoticz if it crashes

Posted: Wednesday 15 March 2017 10:25
by Egregius
Line by line:
1: Is your logfile in /tmp? Otherwise change path or remove line so it's not copied.
2: Domoticz was already stopped
3: No process found to kill on port 8084
4+ Normal behavior if you restart Domoticz on Syno because those modules are already loaded.

Re: Script to restart Domoticz if it crashes

Posted: Wednesday 15 March 2017 11:08
by EdwinK
Trying to work this.

Re: Script to restart Domoticz if it crashes

Posted: Thursday 16 March 2017 18:23
by havnegata
@Jannl I'm qurious to hear about how you implement the Pushover notification?

Re: Script to restart Domoticz if it crashes

Posted: Friday 17 March 2017 7:29
by jannl
I am a bit busy coming days. I will post the code beginning of next week. Just sent me a pm if I seem to forget it

Re: Script to restart Domoticz if it crashes

Posted: Monday 20 March 2017 20:46
by jannl
Below the command that sends a message to my pushover, replace the tokens with your own (2 different).

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`

Re: Script to restart Domoticz if it crashes

Posted: Wednesday 26 April 2017 11:16
by poudenes
wrong topic

Re: Script to restart Domoticz if it crashes

Posted: Tuesday 02 May 2017 21:20
by rfvdboom
Hi,

When my Domoticz crashes/not responding, status says: "active (exited)". The script doesn't work.

So I changed this

elif [[ $DomoticzState == *"inactive (dead)"* ]]

To

elif [[ $DomoticzState != *"active (running)"* ]]

grtz Ralph

Re: Script to restart Domoticz if it crashes

Posted: Wednesday 03 May 2017 9:05
by EdwinK
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.
Installed. Hope it's working.

Re: Script to restart Domoticz if it crashes

Posted: Tuesday 16 May 2017 2:24
by Caribou
G3rard wrote:Thanks @K3rryBlue for sharing the script. Works fine on my Ubuntu Server with some small changes.
Hi G3rard,
Would you mind to share your version including these small changes please?

Re: Script to restart Domoticz if it crashes

Posted: Tuesday 16 May 2017 22:48
by G3rard
Caribou wrote: Hi G3rard,
Would you mind to share your version including these small changes please?
Here it is. Just change the folders and Domoticz URL and port to yours and it should work.

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