Script to restart Domoticz if it crashes Topic is solved

All kinds of 'OS' scripts

Moderator: leecollings

User avatar
PeterCelica
Posts: 21
Joined: Monday 13 March 2017 20:20
Target OS: NAS (Synology & others)
Domoticz version: 3.0.7602
Location: Netherlands
Contact:

Re: Script to restart Domoticz if it crashes

Post by PeterCelica »

Egregius wrote: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.
Hi Egregius, My Domotics Ver. 3.0.7416-7 Jadahl version for Synology DS214Play crashes several times a day,Using RFXCom and Aeotec ZWave Gen5 stick I've seen your script but how do I run this script on my NAS. Thanks already and Best regards Peter.
User avatar
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

Post by Egregius »

What do you mean with 'how do I run it'? With task scheduler of syno.
Make sure the file is executable, set the schedule to start at 0:00 and repeat every minute until 23:59.
User avatar
PeterCelica
Posts: 21
Joined: Monday 13 March 2017 20:20
Target OS: NAS (Synology & others)
Domoticz version: 3.0.7602
Location: Netherlands
Contact:

Re: Script to restart Domoticz if it crashes

Post by PeterCelica »

Hi Egregius thanks for your quick reply, I have never made a script running from my task planner on my syno. So I copied your script to Notepad and changes the adres to my local Syno adres with Domoticz which is 192.168.178.102:8084, I presume I have to do that ? and then I saved it to a local map on my Syno with the extension .sh and then I put it into my task scheduler with a frequentie of 1 Min. I hope I did it all right.

Best regards Peter.
User avatar
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

Post by Egregius »

And logged in with SSH to make it executable?
also think it would been better to create the complete file thru SSH. Not sure if Notepad doesn't screw things up in a linux shell script.
User avatar
PeterCelica
Posts: 21
Joined: Monday 13 March 2017 20:20
Target OS: NAS (Synology & others)
Domoticz version: 3.0.7602
Location: Netherlands
Contact:

Re: Script to restart Domoticz if it crashes

Post by PeterCelica »

Thx for your reply again, and do I need to leave the adres on 127.0.0.1 or was the change I did ok.
User avatar
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

Post by Egregius »

You run it on the same device, so 127.0.0.1 is the best option.
User avatar
PeterCelica
Posts: 21
Joined: Monday 13 March 2017 20:20
Target OS: NAS (Synology & others)
Domoticz version: 3.0.7602
Location: Netherlands
Contact:

Re: Script to restart Domoticz if it crashes

Post by PeterCelica »

Oké Thx for your help, Grtz Peter.
User avatar
PeterCelica
Posts: 21
Joined: Monday 13 March 2017 20:20
Target OS: NAS (Synology & others)
Domoticz version: 3.0.7602
Location: Netherlands
Contact:

Re: Script to restart Domoticz if it crashes

Post by PeterCelica »

Hi Egregius, your script works fine :-)) Domoticz doesn't crash anymore, only the Z-Wave Nodes disappear, I have to restart Domoticz manually to get the Nodes appearing again, reading through this forum it's a known problem sinds the last beta version which I'm using @ this moment, or is there something else I can do about it ??, Best regards Peter.
User avatar
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

Post by Egregius »

If they all disappear you could do something with the json.htm?type=openzwavenodes&idx=3 command (change idx to idx of your zwave controller).
Do that instead of the normal device command, you should change some more code to react on the new output.
User avatar
PeterCelica
Posts: 21
Joined: Monday 13 March 2017 20:20
Target OS: NAS (Synology & others)
Domoticz version: 3.0.7602
Location: Netherlands
Contact:

Re: Script to restart Domoticz if it crashes

Post by PeterCelica »

Hi Egregius, thanks for your reply but I don't have a clue how and were to do this ??

Best regards Peter.
User avatar
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

Post by Egregius »

Something like this should do it:

Code: Select all

#!/bin/bash
DOMOTICZ=`curl -s --connect-timeout 2 --max-time 5 "http://127.0.0.1:8080/json.htm?type=openzwavenodes&idx=3"`
STATUS=`echo $DOMOTICZ | jq -r '.result[1].Manufacturer_name'`
if [ "$STATUS" == "Qubino" ] ; then
	exit
else
	sleep 10
	DOMOTICZ=`curl -s --connect-timeout 2 --max-time 5 "http://127.0.0.1:8080/json.htm?type=openzwavenodes&idx=3"`
	STATUS2=`echo $DOMOTICZ | jq -r '.result[1].Manufacturer_name'`
	if [ "$STATUS2" == "Qubino" ] ; then
		exit
	else
		sleep 10
		DOMOTICZ=`curl -s --connect-timeout 2 --max-time 5 "http://127.0.0.1:8080/json.htm?type=openzwavenodes&idx=3"`
		STATUS3=`echo $DOMOTICZ | jq -r '.result[1].Manufacturer_name'`
		if [ "$STATUS3" == "Qubino" ] ; then
			exit
		else
			sleep 10
			DOMOTICZ=`curl -s --connect-timeout 2 --max-time 5 "http://127.0.0.1:8080/json.htm?type=openzwavenodes&idx=3"`
			STATUS4=`echo $DOMOTICZ | jq -r '.result[1].Manufacturer_name'`
			if [ "$STATUS4" == "Qubino" ] ; 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 /var/log/domoticz.txt /home/pi/domlogs/domoticz-$NOW.txt
				sleep 1
				sudo reboot
				exit
			fi
		fi
	fi
fi
exit
Change the idx in the curl command to the idx of your zwave hardware
jq -r '.result[1].Manufacturer_name'` gives the manufacturer of the 1ste node, in my case a qubino.
I asume that that wont be set if all nodes dissapear from the list.
User avatar
PeterCelica
Posts: 21
Joined: Monday 13 March 2017 20:20
Target OS: NAS (Synology & others)
Domoticz version: 3.0.7602
Location: Netherlands
Contact:

Re: Script to restart Domoticz if it crashes

Post by PeterCelica »

Hi Egregius, thank you ever so much for your programming that you wanna do this for me I appreciate it and I'm gonna try this tomorrow and let you know if this script is working ;-)

Best regards Peter.
User avatar
PeterCelica
Posts: 21
Joined: Monday 13 March 2017 20:20
Target OS: NAS (Synology & others)
Domoticz version: 3.0.7602
Location: Netherlands
Contact:

Re: Script to restart Domoticz if it crashes

Post by PeterCelica »

Hi Egregius, I tried the scrip after I first changed all the Qubino to Aeotec Manufacturer name, but it doesn't work the Nodes still disappear after a while.
I presume I have to put this scrip also in the task manager from my Synology NAS with a run every minute like the other script or is this one different and doesn't work now.

I hope to hear from you again,

Best regards Peter.
User avatar
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

Post by Egregius »

Yes, needs to be scheduled every minute.
User avatar
PeterCelica
Posts: 21
Joined: Monday 13 March 2017 20:20
Target OS: NAS (Synology & others)
Domoticz version: 3.0.7602
Location: Netherlands
Contact:

Re: Script to restart Domoticz if it crashes

Post by PeterCelica »

Oké Thx I gonna try this now ;-)
User avatar
PeterCelica
Posts: 21
Joined: Monday 13 March 2017 20:20
Target OS: NAS (Synology & others)
Domoticz version: 3.0.7602
Location: Netherlands
Contact:

Re: Script to restart Domoticz if it crashes

Post by PeterCelica »

Hi Egregius, I've tried your script on my Syno DS214Play but the Nodes are still disappearing, while your first scrip to stop crashing Domoticz is running oké.
So I don't know whats wrong with it ??

#!/bin/bash
DOMOTICZ=`curl -s --connect-timeout 2 --max-time 5
"http://127.0.0.1:8080/json.htm?type=openzwavenodes&idx=3"`
STATUS=`echo $DOMOTICZ | jq -r
'.result[1].Manufacturer_name'`
if [ "$STATUS" == "Aeotec" ] ; then
exit
else
sleep 10
DOMOTICZ=`curl -s --connect-timeout 2 --max-time 5
"http://127.0.0.1:8080/json.htm?type=openzwavenodes&idx=3"`
STATUS2=`echo $DOMOTICZ | jq -r
'.result[1].Manufacturer_name'`
if [ "$STATUS2" == "Aeotec" ] ; then
exit
else
sleep 10
DOMOTICZ=`curl -s --connect-timeout 2 --max-time 5
"http://127.0.0.1:8080/json.htm?type=openzwavenodes&idx=3"`
STATUS3=`echo $DOMOTICZ | jq -r
'.result[1].Manufacturer_name'`
if [ "$STATUS3" == "Aeotec" ] ; then
exit
else
sleep 10
DOMOTICZ=`curl -s --connect-timeout 2 --max-time 5
"http://127.0.0.1:8080/json.htm?type=openzwavenodes&idx=3"`
STATUS4=`echo $DOMOTICZ | jq -r
'.result[1].Manufacturer_name'`
if [ "$STATUS4" == "Aeotec" ] ; 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 /var/log/domoticz.txt /home/pi/domlogs/domoticz-$NOW.txt
sleep 1
sudo reboot
exit
fi
fi
fi
fi
exit

Grtz Peter.
User avatar
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

Post by Egregius »

Is 3 the idx of your zwave hardware?
What is the manufacturer of the first node? So not the controller, probably node 2 in the list.
User avatar
PeterCelica
Posts: 21
Joined: Monday 13 March 2017 20:20
Target OS: NAS (Synology & others)
Domoticz version: 3.0.7602
Location: Netherlands
Contact:

Re: Script to restart Domoticz if it crashes

Post by PeterCelica »

Hi where can I see the idx is this the Nodeid and Node 1 is the Aeotec Z-Wave stick and Node 2, 3 en 5 are Neo CoolCam powerplugs
User avatar
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

Post by Egregius »

No, the IDX of the hardware under settings->hardware (before you go to the node list).
If node 2 (for me that's the first node as I don't see the controller as a node) is a Neo Coolcam then you should use that as manufacturer.
Please use a code bloc to post a script, no it's unreadable what you have. There must be something else wrong, because with this it should restart domoticz every minute.
For example: do you have a telegram.php file to sent messages? If not remove that line.
Do you need/want to copy the logfile before restarting? If not remove that line.
Keep in mind that in this script the command is "sudo reboot" instead of restarting only the domoticz service. Look at previous pages in this topic for the commands to restart domoticz on a Syno.
User avatar
PeterCelica
Posts: 21
Joined: Monday 13 March 2017 20:20
Target OS: NAS (Synology & others)
Domoticz version: 3.0.7602
Location: Netherlands
Contact:

Re: Script to restart Domoticz if it crashes

Post by PeterCelica »

Hi Engregius thank you ever so much about your explanation,because I don't know anything about Linux programming so I found everything you've explained and changed this, only I have to find the restart bit but that won;t be any problem, I hope Domoticz will keep running now.

Have nice beautiful sunny day and thanks again,

Grtzzzz Peter
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest