Page 6 of 10

Re: Script to restart Domoticz if it crashes

Posted: Sunday 02 July 2017 14:02
by Egregius
The script starts wit the /bin/bash shabang, no need to specify bash. Just execute it.

Re: Script to restart Domoticz if it crashes

Posted: Sunday 09 July 2017 19:55
by underscore4
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?

Re: Script to restart Domoticz if it crashes

Posted: Sunday 09 July 2017 22:14
by Egregius
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.

Re: Script to restart Domoticz if it crashes

Posted: Wednesday 12 July 2017 10:15
by michaldobrotka
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

Re: Script to restart Domoticz if it crashes

Posted: Wednesday 12 July 2017 19:44
by Egregius
Use the script in http://www.domoticz.com/forum/viewtopic ... 80#p137092 but only in cron, not as a service.

Re: Script to restart Domoticz if it crashes

Posted: Thursday 13 July 2017 19:56
by EdwinK
Thanks. Using the one mentioned in the above post.

Re: Script to restart Domoticz if it crashes

Posted: Monday 17 July 2017 8:11
by kniazio
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

Re: Script to restart Domoticz if it crashes

Posted: Monday 17 July 2017 9:16
by Egregius
apt-get install jq ;)

Re: Script to restart Domoticz if it crashes

Posted: Monday 17 July 2017 9:26
by kniazio
Egregius wrote:apt-get install jq ;)
Thanks

Re: Script to restart Domoticz if it crashes

Posted: Monday 31 July 2017 21:49
by HFman
May be i missed it.. but why don't use this way :

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

Monit not a nice tool ?

Re: Script to restart Domoticz if it crashes

Posted: Monday 31 July 2017 22:54
by Egregius
Because a listening port isn't a good way to check if a service is running.

Re: Script to restart Domoticz if it crashes

Posted: Monday 31 July 2017 23:50
by HFman
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.

Re: Script to restart Domoticz if it crashes

Posted: Tuesday 01 August 2017 6:21
by Egregius
Are the lastupdate times still updated then? Otherwise you may do something with that.

Re: Script to restart Domoticz if it crashes

Posted: Tuesday 01 August 2017 11:33
by HFman
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.

Re: Script to restart Domoticz if it crashes

Posted: Tuesday 01 August 2017 22:53
by Egregius
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.

Re: Script to restart Domoticz if it crashes

Posted: Wednesday 02 August 2017 10:10
by HFman
Will see if i can manage that. Will use the scripts in this topic as reference.
Thanks for your feedback :).

Re: Script to restart Domoticz if it crashes

Posted: Thursday 03 August 2017 7:32
by Egregius
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

Re: Script to restart Domoticz if it crashes

Posted: Thursday 03 August 2017 20:18
by HFman
Thanks !

Will try and let you know.

Re: Script to restart Domoticz if it crashes

Posted: Thursday 10 August 2017 19:16
by brommetje
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?

Re: Script to restart Domoticz if it crashes

Posted: Friday 11 August 2017 4:57
by Egregius
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.