Script to restart Domoticz if it crashes

All kinds of 'OS' scripts
Post Reply
fisics
Posts: 80
Joined: Friday 12 July 2013 15:51
Target OS: -
Domoticz version:
Contact:

Script to restart Domoticz if it crashes

Post by fisics » Monday 28 July 2014 13:25

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 11:57, edited 1 time in total.
Reason: Please use [code] tags to improve readability

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

Re: Script to restart Domoticz if it crashes

Post by fisics » Monday 28 July 2014 13:32

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!"

User avatar
Derik
Posts: 2115
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: Script to restart Domoticz if it crashes

Post by Derik » Monday 28 July 2014 13:34

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: 1071
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi
Domoticz version: 3.8807
Location: North East England
Contact:

Re: Script to restart Domoticz if it crashes

Post by simonrg » Monday 28 July 2014 13:35

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
Posts: 2158
Joined: Friday 12 July 2013 21:24
Target OS: Linux
Domoticz version: V4.9866
Location: The Netherlands
Contact:

Re: Script to restart Domoticz if it crashes

Post by SweetPants » Monday 28 July 2014 19:02

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
Ubuntu 16.04.3 LTS, Intel NUC, MySensors 2.3.0-alpha, Pro Mini 3.3/8M RFM69(HW), ESP8266 (SDK2.2.0), Sonoff, RFLink, RFXcom, MQTT, NodeRed, Domoticz Beta (source)

nigels0
Posts: 231
Joined: Thursday 23 January 2014 13:43
Target OS: Raspberry Pi
Domoticz version: 3.8153
Contact:

Re: Script to restart Domoticz if it crashes

Post by nigels0 » Wednesday 13 August 2014 23:51

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: 751
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 » Thursday 14 August 2014 8:23

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: 231
Joined: Thursday 23 January 2014 13:43
Target OS: Raspberry Pi
Domoticz version: 3.8153
Contact:

Re: Script to restart Domoticz if it crashes

Post by nigels0 » Thursday 14 August 2014 22:40

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: 940
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi
Domoticz version: 4.9700
Location: Netherlands
Contact:

Re: Script to restart Domoticz if it crashes

Post by sincze » Wednesday 14 January 2015 11:31

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
Using Pass2php since 2016-12
LAN: RFLink, P1-Port, OTGW, MySensors
USB: RFXCom, ZWave
WIFI: Mi-light Wifi-Bridge, Sonoff, ESP8266, Xiaomi Gateway
Solar: Omnik Inverter, PVOutput
Video: Kodi clients with Harmony HUB
Sensors: You name it I probably got 1.

User avatar
ThinkPad
Posts: 1754
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 » Wednesday 14 January 2015 18:32

Moved topic to 'Scripts' section.
ThinkTheme - theme for Domoticz
My (Dutch) blog: http://thinkpad.tweakblogs.net - My Domoticz scripts: Bitbucket
I'm not (very) active anymore on this forum as i don't use Domoticz anymore.

frave
Posts: 32
Joined: Thursday 08 January 2015 9:53
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Script to restart Domoticz if it crashes

Post by frave » Thursday 11 June 2015 11:48

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.

User avatar
Derik
Posts: 2115
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: Script to restart Domoticz if it crashes

Post by Derik » Thursday 11 June 2015 18:22

@ 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: 561
Joined: Wednesday 26 February 2014 16:28
Target OS: Raspberry Pi
Domoticz version:
Location: the Netherlands
Contact:

Re: Script to restart Domoticz if it crashes

Post by roblom » Thursday 11 June 2015 19:05

There is also an example of a script in the monit wiki.

Ingmar
Posts: 68
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 » Monday 22 June 2015 10:01

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: 32
Joined: Thursday 08 January 2015 9:53
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Script to restart Domoticz if it crashes

Post by frave » Monday 22 June 2015 15:16

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: 32
Joined: Thursday 08 January 2015 9:53
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Script to restart Domoticz if it crashes

Post by frave » Friday 30 October 2015 18:35

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: 2
Joined: Sunday 01 November 2015 23:45
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Script to restart Domoticz if it crashes

Post by HvdW » Sunday 01 November 2015 23:52

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

User avatar
Derik
Posts: 2115
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: Script to restart Domoticz if it crashes

Post by Derik » Sunday 15 November 2015 9:23

@ 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

User avatar
Derik
Posts: 2115
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: Script to restart Domoticz if it crashes

Post by Derik » Sunday 15 November 2015 12:08

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 24520 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: 170
Joined: Sunday 11 December 2016 14:51
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Script to restart Domoticz if it crashes

Post by tjabas » Monday 19 December 2016 18:22

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 4 guests