Script to restart Domoticz if it crashes Topic is solved

All kinds of 'OS' scripts

Moderator: leecollings

jannl
Posts: 625
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Geleen
Contact:

Re: Script to restart Domoticz if it crashes

Post by jannl »

I'll post my script when I am at home tonight or tomorrow
User avatar
K3rryBlue
Posts: 77
Joined: Tuesday 07 February 2017 22:13
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.10xx
Location: Netherlands
Contact:

Re: Script to restart Domoticz if it crashes

Post 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..................
Last edited by K3rryBlue on Friday 10 March 2017 10:54, edited 1 time in total.
Domoticz V4.10717, RPi2 -> Linux raspberrypi 4.4.47-v7+ , RFXCOM - RFXtrx433 USB 433.92MHz ,RAZBERRY2 and OpenZWave USB Version: 1.4-2363-g1f10253-dirty
uc530
Posts: 6
Joined: Tuesday 07 March 2017 0:27
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Script to restart Domoticz if it crashes

Post 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...
User avatar
K3rryBlue
Posts: 77
Joined: Tuesday 07 February 2017 22:13
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.10xx
Location: Netherlands
Contact:

Re: Script to restart Domoticz if it crashes

Post 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
Domoticz V4.10717, RPi2 -> Linux raspberrypi 4.4.47-v7+ , RFXCOM - RFXtrx433 USB 433.92MHz ,RAZBERRY2 and OpenZWave USB Version: 1.4-2363-g1f10253-dirty
jannl
Posts: 625
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Geleen
Contact:

Re: Script to restart Domoticz if it crashes

Post by jannl »

Basically that is what my script does as well
User avatar
Egregius
Posts: 2582
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

Post 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.
User avatar
K3rryBlue
Posts: 77
Joined: Tuesday 07 February 2017 22:13
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.10xx
Location: Netherlands
Contact:

Re: Script to restart Domoticz if it crashes

Post by K3rryBlue »

Hi Egregius,

Seems promising, I will look into it and adapt it for the rPi.
Domoticz V4.10717, RPi2 -> Linux raspberrypi 4.4.47-v7+ , RFXCOM - RFXtrx433 USB 433.92MHz ,RAZBERRY2 and OpenZWave USB Version: 1.4-2363-g1f10253-dirty
User avatar
K3rryBlue
Posts: 77
Joined: Tuesday 07 February 2017 22:13
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.10xx
Location: Netherlands
Contact:

Re: Script to restart Domoticz if it crashes

Post 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
Domoticz V4.10717, RPi2 -> Linux raspberrypi 4.4.47-v7+ , RFXCOM - RFXtrx433 USB 433.92MHz ,RAZBERRY2 and OpenZWave USB Version: 1.4-2363-g1f10253-dirty
User avatar
G3rard
Posts: 669
Joined: Wednesday 04 March 2015 22:15
Target OS: -
Domoticz version: No
Location: The Netherlands
Contact:

Re: Script to restart Domoticz if it crashes

Post by G3rard »

Thanks @K3rryBlue for sharing the script. Works fine on my Ubuntu Server with some small changes.
Not using Domoticz anymore
User avatar
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

Post 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.
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | Hue | Tuya | IKEA tradfri | Dashticz V3 on Lenovo Huawei Tablet | Conbee
User avatar
Egregius
Posts: 2582
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

Post 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.
User avatar
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

Post by EdwinK »

Trying to work this.
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | Hue | Tuya | IKEA tradfri | Dashticz V3 on Lenovo Huawei Tablet | Conbee
User avatar
havnegata
Posts: 114
Joined: Wednesday 10 September 2014 11:05
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.10162
Location: Norway
Contact:

Re: Script to restart Domoticz if it crashes

Post by havnegata »

@Jannl I'm qurious to hear about how you implement the Pushover notification?
jannl
Posts: 625
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Geleen
Contact:

Re: Script to restart Domoticz if it crashes

Post 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
jannl
Posts: 625
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Geleen
Contact:

Re: Script to restart Domoticz if it crashes

Post 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`
poudenes
Posts: 667
Joined: Wednesday 08 March 2017 9:42
Target OS: Linux
Domoticz version: 3.8993
Location: Amsterdam
Contact:

Re: Script to restart Domoticz if it crashes

Post by poudenes »

wrong topic
RPi3 B+, Debain Stretch, Domoticz, Homebridge, Dashticz, RFLink, Milight, Z-Wave, Fibaro, Nanoleaf, Nest, Harmony Hub, Now try to understand pass2php
rfvdboom
Posts: 38
Joined: Tuesday 20 September 2016 8:32
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Netherlands
Contact:

Re: Script to restart Domoticz if it crashes

Post 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
Odroid N2+ | Raspberry P 3 ModelB | RFXcom | AEOTEC Zwave | Honeywell Lyric T6| Milight | Milighthub (NodeMCU) | SolarEdge | P1 Smart meter USB | KAKU | Wemos D1 (esphome) | Denon | Logitech Harmony
User avatar
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

Post 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.
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | Hue | Tuya | IKEA tradfri | Dashticz V3 on Lenovo Huawei Tablet | Conbee
Caribou
Posts: 9
Joined: Thursday 20 April 2017 17:35
Target OS: Linux
Domoticz version:
Location: Bordeaux
Contact:

Re: Script to restart Domoticz if it crashes

Post 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?
User avatar
G3rard
Posts: 669
Joined: Wednesday 04 March 2015 22:15
Target OS: -
Domoticz version: No
Location: The Netherlands
Contact:

Re: Script to restart Domoticz if it crashes

Post 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
Not using Domoticz anymore
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest