Re: Script to restart Domoticz if it crashes
Posted: Tuesday 03 October 2017 19:41
yep i use only a cable and no wifi..
Are tere the solutions to?
Are tere the solutions to?
Open source Home Automation System
https://forum.domoticz.com/
Code: Select all
11 4/12/20 * * * sudo reboot
Code: Select all
#!/bin/bash
while true; do
domoticzcheck=$(curl --max-time 60 -k -s "https://USERNAME:[email protected]/json.htm?type=command¶m=getversion" | grep '"status"' | awk '{ print $3 }' | sed 's/[\,"]//g')
if [[ $domoticzcheck != "OK" ]]; then
echo "Domoticz not running, checking again in 30 seconds."
sleep 30
domoticzcheck=$(curl --max-time 60 -k -s "https://USERNAME:[email protected]/json.htm?type=command¶m=getversion" | grep '"status"' | awk '{ print $3 }' | sed 's/[\,"]//g')
if [[ $domoticzcheck != "OK" ]]; then
echo "Watchguard is restarting Domoticz"
service domoticz stop
service domoticz start
fi
else
echo "Domoticz is running..."
fi
sleep 10
done
mmm please can you tell me the different s between the other?ben53252642 wrote: ↑Wednesday 07 February 2018 1:24 I wrote an alternate version:
Note:Code: Select all
#!/bin/bash while true; do domoticzcheck=$(curl --max-time 60 -k -s "https://USERNAME:[email protected]/json.htm?type=command¶m=getversion" | grep '"status"' | awk '{ print $3 }' | sed 's/[\,"]//g') if [[ $domoticzcheck != "OK" ]]; then echo "Domoticz not running, checking again in 30 seconds." sleep 30 domoticzcheck=$(curl --max-time 60 -k -s "https://USERNAME:[email protected]/json.htm?type=command¶m=getversion" | grep '"status"' | awk '{ print $3 }' | sed 's/[\,"]//g') if [[ $domoticzcheck != "OK" ]]; then echo "Watchguard is restarting Domoticz" service domoticz stop service domoticz start fi else echo "Domoticz is running..." fi sleep 10 done
The curl command is set to allow unsigned SSL certificates.
Code: Select all
#!/bin/sh
### BEGIN INIT INFO
# Provides: watchguard
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 2 3 5
# Default-Stop:
# Description: Starts Watchguard
### END INIT INFO
case "$1" in
'start')
sudo /usr/bin/screen -S watchguard -d -m sudo /root/scripts/watchguard/watchguard.sh
;;
'stop')
sudo pkill -f "watchguard"
;;
*)
echo "Usage: $0 { start | stop }"
;;
esac
exit 0
Code: Select all
cd domoticz/scripts
Code: Select all
nano restart_domoticz.sh
Code: Select all
#!/bin/bash
IsDomoticzRunning=`sudo systemctl status domoticz.service`
if [[ $IsDomoticzRunning != *"active (running)"* ]]
then
echo 'Domoticz is not running.Restarting Domoticz.'
sudo systemctl stop domoticz.service
sudo systemctl restart homebridge.service
echo "Domoticz & Homebridge restarted."
else
echo "Domoticz is running. Nothing to do."
fi
Code: Select all
chmod +x restart_domoticz.sh
Code: Select all
./restart_domoticz.sh
Code: Select all
crontab -e
Code: Select all
*/1 * * * * ./home/pi/domoticz/scripts/restart_domoticz.sh
Absolutely correct! You must use an alternate method that tests if the service is actually running.
It works fine over HTTPS, make sure your Domoticz is set to use basic auth, I've got at least a dozen scripts working this way.ayasystems wrote: ↑Friday 28 September 2018 14:18 Sometimes domoticz is not working, www server of domoticz is not working but systemctl status domoticz shows it as working so you can not check by this way...
Check doing a curl is a better option but you can not use this
domoticzcheck=$(curl --max-time 60 -k -s "https://USERNAME:[email protected]/j ... getversion" | grep '"status"' | awk '{ print $3 }' | sed 's/[\,"]//g')
If you are ussing HTTPS you can not send user passsoword as you are doing... Your script needs pass the info over parameters
Regards!
Every time Domoticz web server was down but service status still looking OK, a check with httping was enough. As I'm doing the check on the PI running Domoticz itself, I only httping usual HTTP port (not secured one) for port default 8080 on localhost.ayasystems wrote: ↑Friday 28 September 2018 14:18 Check doing a curl is a better option but you can not use this
domoticzcheck=$(curl --max-time 60 -k -s "https://USERNAME:[email protected]/j ... getversion" | grep '"status"' | awk '{ print $3 }' | sed 's/[\,"]//g')
lost wrote: ↑Monday 01 October 2018 7:20I would like to see this script ( if you are still active on the forum )ayasystems wrote: ↑Friday 28 September 2018 14:18 <..>
This is runned every X minutes by a cron. Not at home for now, but if someone finds usefull I could send the script that proved successful (with it's httping timeouts/retry setup before giving up to reboot) for me.
<..>