Script to restart Domoticz if it crashes Topic is solved

All kinds of 'OS' scripts

Moderator: leecollings

Post Reply
User avatar
PeterCelica
Posts: 21
Joined: Monday 13 March 2017 20:20
Target OS: NAS (Synology & others)
Domoticz version: 3.0.7602
Location: Netherlands
Contact:

Re: Script to restart Domoticz if it crashes

Post by PeterCelica »

Hi Engregius Yessssssssssssss It's working now Finally Thx for all the good work and help :-) Topppppppppppppp !!!!!
User avatar
PeterCelica
Posts: 21
Joined: Monday 13 March 2017 20:20
Target OS: NAS (Synology & others)
Domoticz version: 3.0.7602
Location: Netherlands
Contact:

Re: Script to restart Domoticz if it crashes

Post by PeterCelica »

This is the final script ;-)

#!/bin/bash
DOMOTICZ=`curl -s --connect-timeout 2 --max-time 5 "http://127.0.0.1:8080/json.htm?type=ope ... odes&idx=4"`
STATUS=`echo $DOMOTICZ | jq -r '.result[1].Manufacturer_name'`
if [ "$STATUS" == "Neo CoolCam" ] ; then
exit
else
sleep 10
DOMOTICZ=`curl -s --connect-timeout 2 --max-time 5 "http://127.0.0.1:8080/json.htm?type=ope ... odes&idx=4"`
STATUS2=`echo $DOMOTICZ | jq -r '.result[1].Manufacturer_name'`
if [ "$STATUS2" == "Neo CoolCam" ] ; then
exit
else
sleep 10
DOMOTICZ=`curl -s --connect-timeout 2 --max-time 5 "http://127.0.0.1:8080/json.htm?type=ope ... odes&idx=4"`
STATUS3=`echo $DOMOTICZ | jq -r '.result[1].Manufacturer_name'`
if [ "$STATUS3" == "Neo CoolCam" ] ; then
exit
else
sleep 10
DOMOTICZ=`curl -s --connect-timeout 2 --max-time 5 "http://127.0.0.1:8080/json.htm?type=ope ... odes&idx=4"`
STATUS4=`echo $DOMOTICZ | jq -r '.result[1].Manufacturer_name'`
if [ "$STATUS4" == "Neo CoolCam" ] ; then
exit
else
curl -s --connect-timeout 2 --max-time 5 --data-urlencode "text=Domoticz Bad - Restarting" --data "silent=false" http://127.0.0.1/secure/telegram.php
NOW=$(date +"%Y-%m-%d_%H%M%S")
cp /tmp/domoticz.log /volume1/files/temp/domoticz-$NOW.txt
sudo /var/packages/domoticz/scripts/start-stop-status stop
sleep 8
sudo kill $(sudo netstat -anp | awk '/ LISTEN / {if($4 ~ ":8084$") { gsub("/.*","",$7); print $7; exit } }')
sleep 8
sudo /var/packages/domoticz/scripts/start-stop-status start
fi
fi
fi
fi
exit
Derik
Posts: 1601
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: Script to restart Domoticz if it crashes

Post by Derik »

Hope some one can help me...
Do have a script to reboot the Pi.

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
With a Cron:

Code: Select all

*/2 * * * * sudo /home/pi/scripts/restart_domoticz.sh 
Only a strange thing...
With the cli the pi is thinking it is running.

Only my webgui says offline...
Is there a solution for.???
Attachments
ScreenShot008.jpg
ScreenShot008.jpg (156.54 KiB) Viewed 6158 times
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
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 »

Look at the scripts provided earlier in this topic.
Derik
Posts: 1601
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: Script to restart Domoticz if it crashes

Post by Derik »

Egregius wrote:Look at the scripts provided earlier in this topic.
Thanks for the great help..

Perhaps it is the same..
And i see now a other problem why it is not working.
The cli says a other thing then my Webgui...
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
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 »

It's not the same. Your script checks the status of the service, notthe actual response of the json api.
Derik
Posts: 1601
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: Script to restart Domoticz if it crashes

Post by Derik »

Egregius wrote:It's not the same. Your script checks the status of the service, notthe actual response of the json api.
Ok only the strange part...
Do this [ the same] check by my Odriod Xu4.... and this is working great.
Only not by my { slave ] Pi.

What is then the best simple script [ without any message etc ] to use
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
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 »

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

Re: Script to restart Domoticz if it crashes

Post by Derik »

'@ Egregius
Thanks for the support.
Only i do not understand your version.
What are things i need to change?
The ip i understand.
U use a message i not?
What idx can i use?
The cpu of the pi?
and do i need a pad to a log script??

Is there no wiki?
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
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 »

This is the stripped version without notification and no log copy.
Change ip:port, rid= idx of a switch.

Code: Select all

#!/bin/bash
DOMOTICZ=`curl -s --connect-timeout 2 --max-time 5 "http://127.0.0.1:8084/json.htm?type=devices&rid=1"`
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:8084/json.htm?type=devices&rid=1"`
   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:8084/json.htm?type=devices&rid=1"`
      STATUS3=`echo $DOMOTICZ | jq -r '.status'`
      if [ "$STATUS3" == "OK" ] ; then
         exit
      else
         sudo /var/packages/domoticz/scripts/start-stop-status stop
         sleep 8
         sudo kill $(sudo netstat -anp | awk '/ LISTEN / {if($4 ~ ":8084$") { gsub("/.*","",$7); print $7; exit } }')
         sleep 8
         sudo /var/packages/domoticz/scripts/start-stop-status start
      fi
   fi
fi
This is for restart on a Synology (change port number):

Code: Select all

sudo /var/packages/domoticz/scripts/start-stop-status stop
sleep 8
sudo kill $(sudo netstat -anp | awk '/ LISTEN / {if($4 ~ ":8084$") { gsub("/.*","",$7); print $7; exit } }')
sleep 8
sudo /var/packages/domoticz/scripts/start-stop-status start
And for restart on a Pi (change port number):

Code: Select all

sudo service domoticz.sh stop
sleep 8
sudo kill $(sudo netstat -anp | awk '/ LISTEN / {if($4 ~ ":8084$") { gsub("/.*","",$7); print $7; exit } }')
sleep 8
sudo service domoticz.sh start
Derik
Posts: 1601
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: Script to restart Domoticz if it crashes

Post by Derik »

Dear E...

Thanks for the great support..
Only i am a dombo...

This part for :
sudo /var/packages/domoticz/scripts/start-stop-status stop\
Is only fr nas or something like that i think...

The pi version i will try..
I think i have to replace the part after : else

Only what idx can i take for the best?
The cpu ?
Or?
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
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 »

Any idx should do, as long as it is a used one. You can easily test by pasting the url in a browser.
Either you use the syno oines, or you replace those by the pi lines.
Derik
Posts: 1601
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: Script to restart Domoticz if it crashes

Post by Derik »

E Thanks!!!!
Works like a charme....!!
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
stephenmhall
Posts: 13
Joined: Saturday 10 June 2017 22:23
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Script to restart Domoticz if it crashes

Post by stephenmhall »

On my Synology I had to add my username and password into the curl command to get it to work, otherwise just got an unauthorized return.
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 »

You could also add 127.0.0.1 to the range that doesn't need to authenticate.
andi216
Posts: 46
Joined: Tuesday 14 March 2017 13:10
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.7243
Contact:

Re: Script to restart Domoticz if it crashes

Post by andi216 »

Hello Egregius

(rpi v2, domoticz v3.8015)
I am not a programmer, I just read and try to apply what I understand.
I'm trying to use the script, but this script restarts domoticz at the crontab time.
*/2 * * * * /home/pi/scripts/restart_domo.sh

During restart, they lose sensor actions.
I'm wrong somewhere???

My Dummy device (idx 6)
Status is OK but restarts domoticz at 2 minutes

http://192.168.2.173:8080/json.htm?type=devices&rid=6

}
],
"status" : "OK",
"title" : "Devices"
}
---------------------------------------------------------

#!/bin/bash
DOMOTICZ=`curl -s --connect-timeout 2 --max-time 5 "http://192.168.2.173:8080/json.htm?type=devices&rid=6"`
STATUS=`echo $DOMOTICZ | jq -r '.status'`
if [ "$STATUS" == "OK" ] ; then
exit
else
sleep 5
DOMOTICZ=`curl -s --connect-timeout 2 --max-time 5 "http://192.168.2.173:8080/json.htm?type=devices&rid=6"`
STATUS2=`echo $DOMOTICZ | jq -r '.status'`
if [ "$STATUS2" == "OK" ] ; then
exit
else
sleep 5
DOMOTICZ=`curl -s --connect-timeout 2 --max-time 5 "http://192.168.2.173:8080/json.htm?type=devices&rid=6"`
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 ~ ":8080$") { gsub("/.*","",$7); print $7; exit } }')
sleep 8
sudo service domoticz.sh start
fi
fi
fi

---------------------------------------------------------------
andi216
Posts: 46
Joined: Tuesday 14 March 2017 13:10
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.7243
Contact:

Re: Script to restart Domoticz if it crashes

Post by andi216 »

My fault. After installing jq everything works fine.
tequila
Posts: 60
Joined: Tuesday 02 August 2016 17:08
Target OS: -
Domoticz version:
Contact:

Re: Script to restart Domoticz if it crashes

Post by tequila »

Hi guys,

I used the script above for RPi2, my url and idx are working fine (getting status OK in a browser).
Jq installed and working.
Yet my domoticz is restarted every time.

Getting this when executing the script manually:

Code: Select all

pi@HOMECTRL01:~/domoticz/scripts/bash$ sh restart_domoticz.sh
restart_domoticz.sh: 4: [: OK: unexpected operator
restart_domoticz.sh: 10: [: OK: unexpected operator
restart_domoticz.sh: 16: [: OK: unexpected operator

Usage:
 kill [options] <pid> [...]

Options:
 <pid> [...]            send signal to every <pid> listed
 -<signal>, -s, --signal <signal>
                        specify the <signal> to be sent
 -l, --list=[<signal>]  list all signal names, or convert one to a name
 -L, --table            list all signal names in a nice table

 -h, --help     display this help and exit
 -V, --version  output version information and exit
Any idea what could be the issue?
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 »

Copy/paste to a windows computer?
tequila
Posts: 60
Joined: Tuesday 02 August 2016 17:08
Target OS: -
Domoticz version:
Contact:

Re: Script to restart Domoticz if it crashes

Post by tequila »

Ok, figured out what the issue was.
I did not know that there are differences in 'sh' and 'bash' syntax. I am quite new to Linux.
Everything seems to be running fine now when executing as 'bash script name.sh' and not 'sh script_name.sh'
Thanks for sharing the script!
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests