Page 5 of 10
Re: Script to restart Domoticz if it crashes
Posted: Friday 26 May 2017 15:11
by PeterCelica
Hi Engregius Yessssssssssssss It's working now Finally Thx for all the good work and help

Topppppppppppppp !!!!!
Re: Script to restart Domoticz if it crashes
Posted: Friday 26 May 2017 15:13
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
Re: Script to restart Domoticz if it crashes
Posted: Sunday 04 June 2017 16:08
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.???
Re: Script to restart Domoticz if it crashes
Posted: Sunday 04 June 2017 16:21
by Egregius
Look at the scripts provided earlier in this topic.
Re: Script to restart Domoticz if it crashes
Posted: Sunday 04 June 2017 16:23
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...
Re: Script to restart Domoticz if it crashes
Posted: Sunday 04 June 2017 17:45
by Egregius
It's not the same. Your script checks the status of the service, notthe actual response of the json api.
Re: Script to restart Domoticz if it crashes
Posted: Sunday 04 June 2017 17:57
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
Re: Script to restart Domoticz if it crashes
Posted: Sunday 04 June 2017 18:22
by Egregius
Re: Script to restart Domoticz if it crashes
Posted: Sunday 04 June 2017 18:56
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?
Re: Script to restart Domoticz if it crashes
Posted: Monday 05 June 2017 6:40
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
Re: Script to restart Domoticz if it crashes
Posted: Monday 05 June 2017 22:11
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?
Re: Script to restart Domoticz if it crashes
Posted: Monday 05 June 2017 22:15
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.
Re: Script to restart Domoticz if it crashes
Posted: Monday 05 June 2017 22:49
by Derik
E Thanks!!!!
Works like a charme....!!
Re: Script to restart Domoticz if it crashes
Posted: Friday 30 June 2017 0:53
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.
Re: Script to restart Domoticz if it crashes
Posted: Friday 30 June 2017 10:34
by Egregius
You could also add 127.0.0.1 to the range that doesn't need to authenticate.
Re: Script to restart Domoticz if it crashes
Posted: Friday 30 June 2017 10:38
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
---------------------------------------------------------------
Re: Script to restart Domoticz if it crashes
Posted: Saturday 01 July 2017 11:44
by andi216
My fault. After installing jq everything works fine.
Re: Script to restart Domoticz if it crashes
Posted: Sunday 02 July 2017 9:32
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?
Re: Script to restart Domoticz if it crashes
Posted: Sunday 02 July 2017 12:51
by Egregius
Copy/paste to a windows computer?
Re: Script to restart Domoticz if it crashes
Posted: Sunday 02 July 2017 13:24
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!