Script to restart Domoticz if it crashes

All kinds of 'OS' scripts
User avatar
Egregius
Posts: 2667
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: Beta
Location: Beitem, BE
Contact:

Re: Script to restart Domoticz if it crashes

Post by Egregius » Sunday 02 July 2017 14:02

The script starts wit the /bin/bash shabang, no need to specify bash. Just execute it.

underscore4
Posts: 12
Joined: Sunday 19 March 2017 23:06
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Script to restart Domoticz if it crashes

Post by underscore4 » Sunday 09 July 2017 19:55

Hi, i'm try to run this code on OrangePI but i get error below:

Code: Select all

#!/bin/bash
  DomoticzState=`sudo service domoticz.sh 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.sh restart
                  echo 'Domoticz restarted.'
  fi
Error output
domoticz_state_checker.sh: 5: domoticz_state_checker.sh: [[: not found
domoticz_state_checker.sh: 8: domoticz_state_checker.sh: [[: not found
Any suggestion?

User avatar
Egregius
Posts: 2667
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: Beta
Location: Beitem, BE
Contact:

Re: Script to restart Domoticz if it crashes

Post by Egregius » Sunday 09 July 2017 22:14

Try the script provided earlier in this topic.
It's a better way than just querying the state of a service. A service can be running but dead.

User avatar
michaldobrotka
Posts: 73
Joined: Sunday 01 November 2015 18:21
Target OS: Raspberry Pi
Domoticz version: Beta
Contact:

Re: Script to restart Domoticz if it crashes

Post by michaldobrotka » Wednesday 12 July 2017 10:15

uc530 wrote:Hi!
Script without loop and crontab command works like charm.
Thanks again for help.
script and added all rights:

Code: Select all

#!/bin/bash

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


crontab edit:

Code: Select all

crontab -e
*/1 * * * * /home/pi/domoticz/scripts/domoticz_state_checker.sh
With best regards UC530
Hi Guys,
I am trying to get running this script. Since Problems with Phyton started Months ago I cant get Domoticz running without crash for more than 2 days. I saved a script (using one quoted above), executed it and added it in crontab, and rc. local to auto startup. How can I check it if is it working (via ssh) ? (Sorry I am noob in Linux and cant find for me understandable answer )
Thanks
Domoticz on Raspi 3 and slave on ZeroW (running only with Broadlink plugin), Serial Mysensors GW on Arduino nano, Broadlink RM2, Rehau CO2 USB Stick, RF link 433 MHz, 2x Amazon Echo Dot (Controlicz), ESP2866 witk Espeasy, Sonoff

User avatar
Egregius
Posts: 2667
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: Beta
Location: Beitem, BE
Contact:

Re: Script to restart Domoticz if it crashes

Post by Egregius » Wednesday 12 July 2017 19:44

Use the script in http://www.domoticz.com/forum/viewtopic ... 80#p137092 but only in cron, not as a service.

User avatar
EdwinK
Posts: 1182
Joined: Sunday 22 January 2017 22:46
Target OS: Raspberry Pi
Domoticz version: BETA
Location: Rhoon
Contact:

Re: Script to restart Domoticz if it crashes

Post by EdwinK » Thursday 13 July 2017 19:56

Thanks. Using the one mentioned in the above post.
Running latest BETA on a Pi-3 | Toon Thermostat | RFxcom | Dashticz V2 |

kniazio
Posts: 74
Joined: Thursday 06 October 2016 8:14
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Script to restart Domoticz if it crashes

Post by kniazio » Monday 17 July 2017 8:11

After handy execution of the script I have such errors

Code: Select all

root@osmc:/home/domoticz# ./domoticz_state_checker.sh                           
./domoticz_state_checker.sh: line 3: jq: command not found
./domoticz_state_checker.sh: line 9: jq: command not found
./domoticz_state_checker.sh: line 15: jq: command not found
My script

Code: Select all

#!/bin/bash
DOMOTICZ=`curl -s --connect-timeout 2 --max-time 5 "http://127.0.0.1:8078/json.htm?type=devices&rid=43"`
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:8078/json.htm?type=devices&rid=43"`
   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:8078/json.htm?type=devices&rid=43"`
      STATUS3=`echo $DOMOTICZ | jq -r '.status'`
      if [ "$STATUS3" == "OK" ] ; then
         exit
      else
sudo service domoticz.sh stop
sleep 8
sudo kill $(sudo netstat -anp | awk '/ LISTEN / {if($4 ~ ":8078$") { gsub("/.*","",$7); print $7; exit } }')
sleep 8
sudo service domoticz.sh start
fi
fi
fi
Please help

User avatar
Egregius
Posts: 2667
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: Beta
Location: Beitem, BE
Contact:

Re: Script to restart Domoticz if it crashes

Post by Egregius » Monday 17 July 2017 9:16

apt-get install jq ;)

kniazio
Posts: 74
Joined: Thursday 06 October 2016 8:14
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Script to restart Domoticz if it crashes

Post by kniazio » Monday 17 July 2017 9:26

Egregius wrote:apt-get install jq ;)
Thanks

HFman
Posts: 37
Joined: Thursday 02 March 2017 21:28
Target OS: Linux
Domoticz version: 3.6635
Contact:

Re: Script to restart Domoticz if it crashes

Post by HFman » Monday 31 July 2017 21:49

May be i missed it.. but why don't use this way :

https://www.domoticz.com/wiki/Monitoring_domoticz

Monit not a nice tool ?
Raspberry PI 2 - Opentherm Gateway + ESP8826 - EvoHome - ESPEasy - RF-Link -

User avatar
Egregius
Posts: 2667
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: Beta
Location: Beitem, BE
Contact:

Re: Script to restart Domoticz if it crashes

Post by Egregius » Monday 31 July 2017 22:54

Because a listening port isn't a good way to check if a service is running.

HFman
Posts: 37
Joined: Thursday 02 March 2017 21:28
Target OS: Linux
Domoticz version: 3.6635
Contact:

Re: Script to restart Domoticz if it crashes

Post by HFman » Monday 31 July 2017 23:50

Read more now, and understand.

I restart the domoticz service every 24 hr (crontab) but still have that domoticz website is perfectly running but the logging (all devices) is after aprox 1 week stopped.
If i then restart domoticz again it will work agian aprox for 1 week.

Which is sligtly worse as without the crontab start/stop action.

Can we not make something in this way :

Make a " device " in Domoticz which we connect with a random generator (or simply a clock device).
If the random generator showes few times the same value, then restart domoticz.
Raspberry PI 2 - Opentherm Gateway + ESP8826 - EvoHome - ESPEasy - RF-Link -

User avatar
Egregius
Posts: 2667
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: Beta
Location: Beitem, BE
Contact:

Re: Script to restart Domoticz if it crashes

Post by Egregius » Tuesday 01 August 2017 6:21

Are the lastupdate times still updated then? Otherwise you may do something with that.

HFman
Posts: 37
Joined: Thursday 02 March 2017 21:28
Target OS: Linux
Domoticz version: 3.6635
Contact:

Re: Script to restart Domoticz if it crashes

Post by HFman » Tuesday 01 August 2017 11:33

In the log i don't see anything any more, but I know that for example, my ESP easy still data sends (via MQTT), and Evohome is also still running.
The data is only not logged/displayed in Domoticz, Domoticz keeps on showing the same value. (so get a flat line), bu the webpage is still running perfectly.
After sudo stop domoticz, sudo start domoticz, everything works gain as it should.
Raspberry PI 2 - Opentherm Gateway + ESP8826 - EvoHome - ESPEasy - RF-Link -

User avatar
Egregius
Posts: 2667
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: Beta
Location: Beitem, BE
Contact:

Re: Script to restart Domoticz if it crashes

Post by Egregius » Tuesday 01 August 2017 22:53

In that case I would get the json of the used devices. Loop thru the lastupdate times and set a variable with the highest value. Then, if variable is more than x minutes ago restart.

HFman
Posts: 37
Joined: Thursday 02 March 2017 21:28
Target OS: Linux
Domoticz version: 3.6635
Contact:

Re: Script to restart Domoticz if it crashes

Post by HFman » Wednesday 02 August 2017 10:10

Will see if i can manage that. Will use the scripts in this topic as reference.
Thanks for your feedback :).
Raspberry PI 2 - Opentherm Gateway + ESP8826 - EvoHome - ESPEasy - RF-Link -

User avatar
Egregius
Posts: 2667
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: Beta
Location: Beitem, BE
Contact:

Re: Script to restart Domoticz if it crashes

Post by Egregius » Thursday 03 August 2017 7:32

In PHP very simple:

Code: Select all

<?php
$domoticz=json_decode(file_get_contents('http://127.0.0.1:8080/json.htm?type=devices&used=true'),true);
if($domoticz){
    $last=0;
    $now=time();
    foreach($domoticz['result'] as $dom){
        if(strtotime($dom['LastUpdate'])>$last)$last=strtotime($dom['LastUpdate']);
    }
    $last=$now-$last;
    echo 'Last update was '.$last.' seconds ago.'.PHP_EOL;
    if($last>300){
        shell_exec('sudo service domoticz restart');
        telegram('Domoticz service restarted, last update was '.$last.' seconds ago.');
    }
}
Run the script from cron, for example:

Code: Select all

* * * * * /usr/bin/php /home/pi/checklastupdate.php

HFman
Posts: 37
Joined: Thursday 02 March 2017 21:28
Target OS: Linux
Domoticz version: 3.6635
Contact:

Re: Script to restart Domoticz if it crashes

Post by HFman » Thursday 03 August 2017 20:18

Thanks !

Will try and let you know.
Raspberry PI 2 - Opentherm Gateway + ESP8826 - EvoHome - ESPEasy - RF-Link -

brommetje
Posts: 53
Joined: Sunday 16 February 2014 18:40
Target OS: Raspberry Pi
Domoticz version: v3.9139
Location: NL
Contact:

Re: Script to restart Domoticz if it crashes

Post by brommetje » Thursday 10 August 2017 19:16

Hi because sometimes my Domoticz crashes I adapt one of the script and changed it a little bit.
However the script is not reliable, ones a day the script says Domoticz is down/crashed but is still running.
I commented out the service start/restart at the moment.

Code: Select all

#!/bin/bash
#
dt=$(date '+%d/%m/%Y %H:%M:%S')
#
LOG=/RAMDISK/domoticz_state_crash.log
#

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
   echo "$dt: OK" | tee -a $LOG
   exit
else
   sleep 5
   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
      echo "$dt: STATUS2=$STATUS2" | tee -a $LOG
      exit
   else
      sleep 5
     #DOMOTICZ=`curl -s --connect-timeout 2 --max-time 5  "http://127.0.0.1:8080/json.htm?type=devices&rid=1"`
      DOMOTICZ=`curl -s --connect-timeout 3 --max-time 10 "http://127.0.0.1:8080/json.htm?type=devices&rid=1"`
      STATUS3=`echo $DOMOTICZ | jq -r '.status'`
      if [ "$STATUS3" == "OK" ] ; then
	 echo "$dt: STATUS3=$STATUS3" | tee -a $LOG
         exit
      else
         ###sudo service domoticz.sh stop
         sleep 8
         ###sudo kill $(sudo netstat -anp | awk '/ LISTEN / {if($4 ~ ":8080$") { gsub("/.*","",$7); print $7; exit } }')
         sleep 8
         ###sudo service domoticz.sh start
         #
         echo "$dt: Domoticz was offline. Restarted Domoticz...." >> "$LOG"
         #
         /home/pi/pushover_shell.sh "`date '+%Y%m%d%H%M'`: Domoticz crashed (v3) but has been restarted !!!" "Crash report script v3"
         #

      fi
   fi
fi
Can it be a timing issue?

User avatar
Egregius
Posts: 2667
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: Beta
Location: Beitem, BE
Contact:

Re: Script to restart Domoticz if it crashes

Post by Egregius » Friday 11 August 2017 4:57

If the script says that domoticz is crashed it means that domoticz didn't respond for 3 times. The 3 tries before restarting is there to prevent falsely restarting.
Must be that your system is sometimes to busy to respond.
Does it restarts always at the same time? Is there something in the log at that time?
Besides that I can only say that I use the script for a very long time on both Synology and Raspberry and it never restarts when not needed.

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests