Page 9 of 10

Re: Script to restart Domoticz if it crashes

Posted: Monday 15 October 2018 17:23
by Egregius

Re: Script to restart Domoticz if it crashes

Posted: Monday 15 October 2018 17:26
by Egregius

Re: Script to restart Domoticz if it crashes

Posted: Wednesday 17 October 2018 18:34
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.

Re: Script to restart Domoticz if it crashes

Posted: Sunday 21 October 2018 19:21
by RapTile
lost wrote: Wednesday 17 October 2018 18:34 <..>
Thanks!

Re: Script to restart Domoticz if it crashes

Posted: Saturday 12 January 2019 20:37
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.

Re: Script to restart Domoticz if it crashes

Posted: Tuesday 21 May 2019 19:54
by amplituda
Hello dear, can you tell me how to make a schedule for a daily restart domoticz at 0:05

Re: Script to restart Domoticz if it crashes

Posted: Tuesday 21 May 2019 20:29
by doh
I followed this guide and moved Domoticz startup to systemd, and it restarts automatically on crash now.

https://randomstuffintech.wordpress.com ... -domoticz/

Re: Script to restart Domoticz if it crashes

Posted: Wednesday 22 May 2019 12:13
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?

Re: Script to restart Domoticz if it crashes

Posted: Wednesday 22 May 2019 12:32
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

Re: Script to restart Domoticz if it crashes

Posted: Wednesday 22 May 2019 15:12
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!)

Re: Script to restart Domoticz if it crashes

Posted: Tuesday 02 July 2019 23:56
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

Re: Script to restart Domoticz if it crashes

Posted: Wednesday 03 July 2019 0:11
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.

Re: Script to restart Domoticz if it crashes

Posted: Wednesday 03 July 2019 8:23
by DosN
Thx @waaren

Tjee that easy, i will add this in the crontab

Re: Script to restart Domoticz if it crashes

Posted: Thursday 19 December 2019 2:15
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 !

Re: Script to restart Domoticz if it crashes

Posted: Thursday 19 December 2019 7:00
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

Re: Script to restart Domoticz if it crashes

Posted: Thursday 19 December 2019 18:22
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 ?

Re: Script to restart Domoticz if it crashes

Posted: Thursday 19 December 2019 18:56
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.

Re: Script to restart Domoticz if it crashes

Posted: Saturday 21 December 2019 1:43
by Carthman
OK Many thanks !

Re: Script to restart Domoticz if it crashes

Posted: Friday 21 February 2020 6:07
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.

Re: Script to restart Domoticz if it crashes

Posted: Friday 21 February 2020 15:52
by Egregius
What is the content of /etc/hosts and /etc/hostname?