Script to restart Domoticz if it crashes Topic is solved

All kinds of 'OS' scripts

Moderator: leecollings

Post Reply
fisics
Posts: 10
Joined: Friday 12 July 2013 15:51
Target OS: -
Domoticz version:
Contact:

Script to restart Domoticz if it crashes

Post by fisics »

Hey guys,

Thought I share a (very basic) script that I use to check if Domoticz has crashed (offline) and restart it, I have this setup on a cron job running every ten minutes.

My script basically launches a Telnet session to Domoticz on port 8080, if it fails to connect it removes the 2 temporary DB files -shm and -wal and relaunches the service.

This code could probably be written in one line by one of you geniuses, but as I'm not a coder - it works for me!

Hope that helps someone else who has frequent offline crashes...

---

Code: Select all

#!/bin/bash

# This is a script to check if Domoticz is running or crashed, if crashed delete the 2 db files and relaunch - my script lives in a folder called domo-alive-test

(
echo open 127.0.0.1 8080
sleep 2
echo "exit"
) | telnet > /home/pi/domo-alive-test/info-output.txt


sleep 5

# Telnet connection check, lets open info-out and make sure it contains the failed telnet output otherwise stop.

grep -Po '\?Invalid command' /home/pi/domo-alive-test/info-output.txt > /home/pi/domo-alive-test/telnet-connection-test.txt

sed -i '1!d' /home/pi/domo-alive-test/telnet-connection-test.txt

info=$(cat /home/pi/domo-alive-test/telnet-connection-test.txt)

rm /home/pi/domo-alive-test/telnet-connection-test.txt

if [ "$info" = "?Invalid command" ]
then
	# Telnet connection refused
	rm /home/pi/domo-alive-test/info-output.txt
	rm /home/pi/domoticz/domoticz.db-wal
	rm /home/pi/domoticz/domoticz.db-shm
	sudo service domoticz.sh start
	exit
else
	exit
fi
Last edited by ThinkPad on Wednesday 14 January 2015 10:57, edited 1 time in total.
Reason: Please use [code] tags to improve readability
fisics
Posts: 10
Joined: Friday 12 July 2013 15:51
Target OS: -
Domoticz version:
Contact:

Re: Script to restart Domoticz if it crashes

Post by fisics »

Just had another thought on this, you could probably do it much easier using:

sudo service domoticz.sh status

To check if it returns

"[FAIL] domoticz is not running ... failed!"
Derik
Posts: 1601
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: Script to restart Domoticz if it crashes

Post by Derik »

looks great..
And better than the watchdog....

Thanks
For sharing :D :D
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: Script to restart Domoticz if it crashes

Post by simonrg »

A different approach is documented on the Wiki using the Watchdog service on the Raspberry pi - http://www.domoticz.com/wiki/Setting_up ... i_watchdog - this runs on the Raspberry Pi, so is self-contained and copes with the Raspberry Pi crashing itself rebooting the Raspberry Pi.

@Derik why Watchdog not good?
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
SweetPants

Re: Script to restart Domoticz if it crashes

Post by SweetPants »

I'm using 'monit' on Raspberry and Ubuntu. You can also use it to monitor/restart other processes like nginx

/etc/monit/conf.d/domoticz.conf

check process domoticz with pidfile /var/run/domoticz.pid
start program "/etc/init.d/domoticz.sh start"
stop program "/etc/init.d/domoticz.sh stop"

/etc/monit/conf.d/nginx.conf

check process nginx with pidfile /var/run/nginx.pid
start program "/etc/init.d/nginx start"
stop program "/etc/init.d/nginx stop"
if failed host 127.0.0.1 port 80
protocol http then restart
if 5 restarts within 5 cycles then timeout

Regards, Harry
nigels0
Posts: 221
Joined: Thursday 23 January 2014 12:43
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Contact:

Re: Script to restart Domoticz if it crashes

Post by nigels0 »

simonrg wrote:A different approach is documented on the Wiki using the Watchdog service on the Raspberry pi - http://www.domoticz.com/wiki/Setting_up ... i_watchdog - this runs on the Raspberry Pi, so is self-contained and copes with the Raspberry Pi crashing itself rebooting the Raspberry Pi.

@Derik why Watchdog not good?
I looked for the domoticz.log file in /tmp, but it doesn't exist - so the watchdog doesn't start up.
User avatar
CopyCatz
Developer
Posts: 123
Joined: Thursday 11 July 2013 17:28
Target OS: -
Domoticz version:
Location: Outer Heaven
Contact:

Re: Script to restart Domoticz if it crashes

Post by CopyCatz »

As stated in the manual, specify the log location in the command line parameters:

-log file_path

Then point the watchdog to the log file.
nigels0
Posts: 221
Joined: Thursday 23 January 2014 12:43
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Contact:

Re: Script to restart Domoticz if it crashes

Post by nigels0 »

I guess the danger of having the log being written to the SD or USB repeatedly is why the log is not switched on by default
User avatar
sincze
Posts: 1300
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: Netherlands / Breda Area
Contact:

Re: Script to restart Domoticz if it crashes

Post by sincze »

nigels0 wrote:I guess the danger of having the log being written to the SD or USB repeatedly is why the log is not switched on by default
I'm writing the log to RAM or NAS. ;-)

It seems that NGINX is crashing a lot. (I configured monit to send me an e-mail so now I know exactly how many times and it is a lot).
10+ times per day it will be restarted by Monit. Is this a known issue? Should I tweak the nginx configuration.
Unfortunately I did not look at the nginx log file to see if it is maybe the known php issue.

https://discussion.dreamhost.com/thread-128870.html

Also Domoticz interface sometimes stalls... several seconds and then moves on again. (Nginx related??)

Sándor
Pass2php
LAN: RFLink, P1, OTGW, MySensors
USB: RFXCom, ZWave, Sonoff 3
MQTT: ZIgbee2MQTT,
ZWAVE: Zwave-JS-UI
WIFI: Mi-light, Tasmota, Xiaomi Shelly
Solar: Omnik, PVOutput
Video: Kodi, Harmony HUB, Chromecast
Sensors: You name it I got 1.
ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: Script to restart Domoticz if it crashes

Post by ThinkPad »

Moved topic to 'Scripts' section.
I am not active on this forum anymore.
frave
Posts: 16
Joined: Thursday 08 January 2015 8:53
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Script to restart Domoticz if it crashes

Post by frave »

Hi, I was looking into a more simpler approach just like fisics opted a few posts back. The reason why is because I found out Domoticz ocassionally crashes. It does not happen a lot but it is frustrating when finding out the moment you would like to flip a switch. I have found a way to interpret the output of "sudo domoticz.sh service status" and it works flawlessly. So to help others with the same problem I thought I will make my first post here.

I just made a file called "domoticz_state_checker.sh" and inserted the following code:

Code: Select all

#!/bin/bash
while true
do

DomoticzState=`sudo service domoticz.sh status`

if [[ $DomoticzState == "domoticz is running." ]]
        then
                echo 'Domoticz is running. Nothing to do.'
elif [[ $DomoticzState == "domoticz is not running ... failed!" ]]
        then
                echo 'Domoticz is not running. Restarting Domoticz...'
                sudo service domoticz.sh restart
                echo 'Domoticz restarted.'
fi

sleep 15
done
I just start the script with "nohup ./domoticz_state_checker.sh &" and it runs in the background. As you can see in the code it checks the state of Domoticz every 15 seconds and when it finds out Domoticz stopped working it performs a simple restart.
Derik
Posts: 1601
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: Script to restart Domoticz if it crashes

Post by Derik »

@ Fisics....

How do you do this?
and where do you place the files??
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups
roblom
Posts: 402
Joined: Wednesday 26 February 2014 15:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: the Netherlands
Contact:

Re: Script to restart Domoticz if it crashes

Post by roblom »

There is also an example of a script in the monit wiki.
Ingmar
Posts: 51
Joined: Sunday 04 May 2014 1:34
Target OS: NAS (Synology & others)
Domoticz version:
Location: The Netherlands
Contact:

Re: Script to restart Domoticz if it crashes

Post by Ingmar »

frave wrote:Hi, I was looking into a more simpler approach just like fisics opted a few posts back. The reason why is because I found out Domoticz ocassionally crashes. It does not happen a lot but it is frustrating when finding out the moment you would like to flip a switch. I have found a way to interpret the output of "sudo domoticz.sh service status" and it works flawlessly. So to help others with the same problem I thought I will make my first post here.

I just made a file called "domoticz_state_checker.sh" and inserted the following code:

Code: Select all

#!/bin/bash
while true
do

DomoticzState=`sudo service domoticz.sh status`

if [[ $DomoticzState == "domoticz is running." ]]
        then
                echo 'Domoticz is running. Nothing to do.'
elif [[ $DomoticzState == "domoticz is not running ... failed!" ]]
        then
                echo 'Domoticz is not running. Restarting Domoticz...'
                sudo service domoticz.sh restart
                echo 'Domoticz restarted.'
fi

sleep 15
done
I just start the script with "nohup ./domoticz_state_checker.sh &" and it runs in the background. As you can see in the code it checks the state of Domoticz every 15 seconds and when it finds out Domoticz stopped working it performs a simple restart.
Thanks, works perfectly!

I added the "nohup /home/pi/domoticz/scripts/domoticz_state_checker.sh &" command in the rc.local file (in /etc) so that it automatically starts when the Pi starts up.
Synology NAS with stable release, AEON Z-wave, RFXCOM, Toon, Echo Dot, HAbridge, HarmonyHUB, Dashticz.
frave
Posts: 16
Joined: Thursday 08 January 2015 8:53
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Script to restart Domoticz if it crashes

Post by frave »

Thanks for your success story :) and of course your addition of the automatic startup. I think I have solved that differently. But your solution is plain and simple.
frave
Posts: 16
Joined: Thursday 08 January 2015 8:53
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Script to restart Domoticz if it crashes

Post by frave »

I am writing this just to inform anyone that wants to use this script and is not running on a Raspberry Pi. I recently migrated Domoticz to new hardware with a standard Debian 8 (jessie) installation so I stopped using my Raspberry Pi for Domoticz. I then came to know the script I wrote above does not work anymore. I therefore edited it as followes.

Code: Select all

#!/bin/bash
while true
do

DomoticzState=`sudo service domoticz 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 restart
                echo 'Domoticz restarted.'
         fi

sleep 15
done
### Done
Then start the script the same way, with the command "nohup ./domoticz_state_checker.sh &", to run in the background.
HvdW
Posts: 538
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

Re: Script to restart Domoticz if it crashes

Post by HvdW »

A perfect script.
Thanks a lot!

This unexpected stall of Domoticz on the Rasperry affects everyone (I think)
The code resolves the problem in a very simple manner.

Code: Select all

  #!/bin/bash
    DomoticzState=`sudo service domoticz.sh status`

    if [[ $DomoticzState == "domoticz is running." ]]
            then
                    echo 'Domoticz is running. Nothing to do.'
    elif [[ $DomoticzState == "domoticz is not running ... failed!" ]]
            then
                    echo 'Domoticz is not running. Restarting Domoticz...'
                    sudo service domoticz.sh restart
                    echo 'Domoticz restarted.'
    fi

sudo chmod +x domoticz_state_checker.sh

crontab -e
*/5 * * * * /home/pi/scripts/domoticz_state_checker.sh

However I removed the loop and made a cronjob which makes the script run every 5 minutes
Bugs bug me.
Derik
Posts: 1601
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: Script to restart Domoticz if it crashes

Post by Derik »

@ HDVW

Very simple script...
I give your srcipt a try, because it is very very simple..

Please Can you make a option when your script is restart Domotic that the board is sending a email?
I think that is a great option
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups
Derik
Posts: 1601
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: Script to restart Domoticz if it crashes

Post by Derik »

strange...
Try the script on my RPi, works great..

Try it on my CB: not...
I have:
ScreenShot042.jpg
ScreenShot042.jpg (36.77 KiB) Viewed 56562 times
I have:

Code: Select all

sudo chmod +x restart_domoticz.sh
I have:

Code: Select all

*/3 * * * * sudo /home/linaro/scripts/restart_domoticz.sh
The same as on my RPi..
I changed the pad..

Some one please, where do i go wrong?

EDIT:
Found a solution..
Changed restart in start
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups
tjabas
Posts: 564
Joined: Sunday 11 December 2016 13:51
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Script to restart Domoticz if it crashes

Post by tjabas »

Im really new to this Linux and rasberry, but how do i install this script?

I have lot of problems with Domoticz freezing, so i guess this script would do the trick.
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests