Script to restart Domoticz if it crashes Topic is solved

All kinds of 'OS' scripts

Moderator: leecollings

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 »

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 »

lost
Posts: 616
Joined: Thursday 10 November 2016 9:30
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Script to restart Domoticz if it crashes

Post by lost »

RapTile wrote: Monday 15 October 2018 13:06 I would like to see this script ( if you are still active on the forum ) ;-)
Hello,

That's this one, named checkDomoticz.sh (under /root):

Code: Select all

#!/bin/bash
# Check domoticz (from a crontab) is up a restart whole PI if needed...

domoticzUrl=localhost:8080
BN=`basename $0`

httping -c 5 -i 0 -t 1 --ts -v -Wsqg $domoticzUrl

if [ $? -ne 0 ]
then
  logger $BN: Domoticz httping-ed KO, retry after 1mn... 
  sleep 1m
  # Retry once
  httping -c 5 -i 0 -t 1 --ts -v -Wsqg $domoticzUrl
  if [ $? -ne 0 ]
  then
    logger $BN: Still KO. Get last logs and REBOOT...
    tail -n 20 /tmp/domoticz.txt | logger
    shutdown -r now
    exit 1
  else
    logger $BN: Retry OK !
  fi
else
  logger $BN: Domoticz ALIVE !
fi
exit 0
Plus the cron to call it (from root, as shutdown needs root privilege) every 30mn:

Code: Select all

# m   h  dom mon dow   command
0,30  *  *   *   *     /root/checkDomoticz.sh > /dev/null 2>&1
That runs independent from Domoticz just to check web interface availability... If not, last 20 log lines from Domoticz (need to activate it, mine logs to /tmp in ram tmpfs) are sent to syslog (for post-mortem info, as /var/log is still on SD card on my system) and PI is rebooted.
RapTile
Posts: 53
Joined: Saturday 10 March 2018 2:01
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Script to restart Domoticz if it crashes

Post by RapTile »

lost wrote: Wednesday 17 October 2018 18:34 <..>
Thanks!
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 »

hi

im a real newbie with this programming stuff, i really want this restart script to work but i have no idea of how to install this on my raspberry pi.
User avatar
amplituda
Posts: 24
Joined: Thursday 23 August 2018 14:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Finland
Contact:

Re: Script to restart Domoticz if it crashes

Post by amplituda »

Hello dear, can you tell me how to make a schedule for a daily restart domoticz at 0:05
doh
Posts: 82
Joined: Monday 01 December 2014 13:28
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: London, UK
Contact:

Re: Script to restart Domoticz if it crashes

Post by doh »

I followed this guide and moved Domoticz startup to systemd, and it restarts automatically on crash now.

https://randomstuffintech.wordpress.com ... -domoticz/
User avatar
amplituda
Posts: 24
Joined: Thursday 23 August 2018 14:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Finland
Contact:

Re: Script to restart Domoticz if it crashes

Post by amplituda »

I do not want to check whether Domoticz has crash, I want to restart it at a certain time. Maybe there is a simple solution?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Script to restart Domoticz if it crashes

Post by waaren »

amplituda wrote: Wednesday 22 May 2019 12:13 I do not want to check whether Domoticz has crash, I want to restart it at a certain time. Maybe there is a simple solution?

Code: Select all

sudo crontab -e
add this line to the crontab

Code: Select all

44 5 * * * service domoticz restart
this will restart domoticz at 05:44 AM
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
amplituda
Posts: 24
Joined: Thursday 23 August 2018 14:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Finland
Contact:

Re: Script to restart Domoticz if it crashes

Post by amplituda »

waaren wrote: Wednesday 22 May 2019 12:32

Code: Select all

sudo crontab -e
add this line to the crontab

Code: Select all

44 5 * * * service domoticz restart
this will restart domoticz at 05:44 AM
Thank you very much! You helped me!)
DosN
Posts: 49
Joined: Monday 29 January 2018 7:58
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Script to restart Domoticz if it crashes

Post by DosN »

Domoticz is not startup after a powerdown of the PI

when i login wit putty and add command
sudo /etc/init.d/domoticz.sh restart

is restart and is running again fine

I have this issue for almost a year now , and hoped a fix will come in the versions..

So i hoped some of you guys can help me..
I found this code interesting, see below. But how can it be triggered , only at start up
When i run this in a crob job it will be triggered every x min, or at a special time
#!/bin/bash
CONFIG=/home/pi/domoticz/scripts/domoticz_state_checker.txt
dt=$(date '+%d/%m/%Y %H:%M:%S')
DomoticzState=`sudo service domoticz.sh status`
if [[ $DomoticzState == *"active (running)"* ]]
then
echo "Domoticz is running. Nothing to do."
elif [[ $DomoticzState == *"inactive (dead)"* ]]
then
echo "$dt, Domoticz is not running. Restarting Domoticz..." >> "$CONFIG"
sudo service domoticz.sh restart
echo "$dt, Domoticz restarted..." >> "$CONFIG"
fi
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Script to restart Domoticz if it crashes

Post by waaren »

DosN wrote: Tuesday 02 July 2019 23:56 Domoticz is not startup after a powerdown of the PI
I found this code interesting, see below. But how can it be triggered , only at start up
When i run this in a crob job it will be triggered every x min, or at a special time
in crontab you could use

Code: Select all

@reboot       sleep 180 && /etc/init.d/domoticz.sh restart
this will restart domoticz 3 minutes after a reboot.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
DosN
Posts: 49
Joined: Monday 29 January 2018 7:58
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Script to restart Domoticz if it crashes

Post by DosN »

Thx @waaren

Tjee that easy, i will add this in the crontab
Carthman
Posts: 31
Joined: Friday 08 February 2019 8:35
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: France
Contact:

Re: Script to restart Domoticz if it crashes

Post by Carthman »

K3rryBlue wrote: Friday 10 March 2017 21:06 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
Hi,

So I used this version for my pi
I can launch the script without errors, but is it not supposed to write in a log something ?
Or maybe it will just write if a restart occured ?

And finally you just have to add this script in the crontab ?

Many thanks !
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Script to restart Domoticz if it crashes

Post by waaren »

Carthman wrote: Thursday 19 December 2019 2:15 So I used this version for my pi. I can launch the script without errors, but is it not supposed to write in a log something ?
Only when it tried to restart domoticz it will write a line in /home/pi/domoticz/scripts/bash/domoticz_state_checker.txt
You first need to create directory /home/pi/domoticz/scripts/bash
And finally you just have to add this script in the crontab ?
Yes
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Carthman
Posts: 31
Joined: Friday 08 February 2019 8:35
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: France
Contact:

Re: Script to restart Domoticz if it crashes

Post by Carthman »

waaren wrote: Thursday 19 December 2019 7:00
Carthman wrote: Thursday 19 December 2019 2:15 So I used this version for my pi. I can launch the script without errors, but is it not supposed to write in a log something ?
Only when it tried to restart domoticz it will write a line in /home/pi/domoticz/scripts/bash/domoticz_state_checker.txt
You first need to create directory /home/pi/domoticz/scripts/bash
Is there a special need to have the log file under /scripts/bash, or can I put it just in /scripts ?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Script to restart Domoticz if it crashes

Post by waaren »

Carthman wrote: Thursday 19 December 2019 18:22 Is there a special need to have the log file under /scripts/bash, or can I put it just in /scripts ?
No you can put it anywhere you like. Of course you need to have enough access rights to write in the directory of choice.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Carthman
Posts: 31
Joined: Friday 08 February 2019 8:35
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: France
Contact:

Re: Script to restart Domoticz if it crashes

Post by Carthman »

OK Many thanks !
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 »

Make a new file, for example /home/pi/checkdomoticz.sh and add this code:

Code: Select all

#!/bin/bash

: '
crontab -e
# m h  dom mon dow   command
* * * * * /usr/bin/nice -n20 /home/pi/checkdomoticz.sh >/dev/null 2>&1
'

DOMOTICZ=`curl -s --connect-timeout 2 --max-time 5 "http://127.0.0.1:8080/json.htm?type=devices&rid=1"`
STATUS=`echo $DOMOTICZ | jq -r '.status'`
if [ "$STATUS" == "OK" ] ; then
 	exit
else
	sleep 20
	DOMOTICZ=`curl -s --connect-timeout 2 --max-time 5 "http://127.0.0.1:8080/json.htm?type=devices&rid=1"`
	STATUS2=`echo $DOMOTICZ | jq -r '.status'`
	if [ "$STATUS2" == "OK" ] ; then
		exit
	else
		sleep 20
		DOMOTICZ=`curl -s --connect-timeout 2 --max-time 5 "http://127.0.0.1:8080/json.htm?type=devices&rid=1"`
		STATUS3=`echo $DOMOTICZ | jq -r '.status'`
		if [ "$STATUS3" == "OK" ] ; then
			exit
		else
			service domoticz stop
			service domoticz start
			#shutdown -r now
		fi
	fi
fi
install jq

Code: Select all

apt install jq
set the file as executable with

Code: Select all

chmod +x /home/pi/checkdomoticz.sh
add it to crontab as indicated in the script.
Of course, alter ip/port if needed.

Warning: best to stop the cron service before updating domoticz from the gui.
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 »

What is the content of /etc/hosts and /etc/hostname?
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests