Re: Script to restart Domoticz if it crashes
Posted: Monday 15 October 2018 17:23
This is a good script : http://www.domoticz.com/forum/viewtopic ... 89#p149685
Open source Home Automation System
https://forum.domoticz.com/
Hello,RapTile wrote: Monday 15 October 2018 13:06 I would like to see this script ( if you are still active on the forum )![]()
Code: Select all
#!/bin/bash
# Check domoticz (from a crontab) is up a restart whole PI if needed...
domoticzUrl=localhost:8080
BN=`basename $0`
httping -c 5 -i 0 -t 1 --ts -v -Wsqg $domoticzUrl
if [ $? -ne 0 ]
then
logger $BN: Domoticz httping-ed KO, retry after 1mn...
sleep 1m
# Retry once
httping -c 5 -i 0 -t 1 --ts -v -Wsqg $domoticzUrl
if [ $? -ne 0 ]
then
logger $BN: Still KO. Get last logs and REBOOT...
tail -n 20 /tmp/domoticz.txt | logger
shutdown -r now
exit 1
else
logger $BN: Retry OK !
fi
else
logger $BN: Domoticz ALIVE !
fi
exit 0
Code: Select all
# m h dom mon dow command
0,30 * * * * /root/checkDomoticz.sh > /dev/null 2>&1
Thanks!
amplituda wrote: Wednesday 22 May 2019 12:13 I do not want to check whether Domoticz has crash, I want to restart it at a certain time. Maybe there is a simple solution?
Code: Select all
sudo crontab -eCode: Select all
44 5 * * * service domoticz restart
Thank you very much! You helped me!)waaren wrote: Wednesday 22 May 2019 12:32add this line to the crontabCode: Select all
sudo crontab -ethis will restart domoticz at 05:44 AMCode: Select all
44 5 * * * service domoticz restart
#!/bin/bash
CONFIG=/home/pi/domoticz/scripts/domoticz_state_checker.txt
dt=$(date '+%d/%m/%Y %H:%M:%S')
DomoticzState=`sudo service domoticz.sh status`
if [[ $DomoticzState == *"active (running)"* ]]
then
echo "Domoticz is running. Nothing to do."
elif [[ $DomoticzState == *"inactive (dead)"* ]]
then
echo "$dt, Domoticz is not running. Restarting Domoticz..." >> "$CONFIG"
sudo service domoticz.sh restart
echo "$dt, Domoticz restarted..." >> "$CONFIG"
fi
in crontab you could useDosN wrote: Tuesday 02 July 2019 23:56 Domoticz is not startup after a powerdown of the PI
I found this code interesting, see below. But how can it be triggered , only at start up
When i run this in a crob job it will be triggered every x min, or at a special time
Code: Select all
@reboot sleep 180 && /etc/init.d/domoticz.sh restartHi,K3rryBlue wrote: Friday 10 March 2017 21:06 This one works for the pi, needed to make more changes then expected.Code: Select all
#!/bin/bash dt=$(date '+%d/%m/%Y %H:%M:%S') CONFIG=/home/pi/domoticz/scripts/bash/domoticz_state_checker.txt STATUS=`curl -s --connect-timeout 2 --max-time 5 "Accept: application/json" "http://192.168.1.109:8080/json.htm?type=devices&rid=1" | grep "status"| awk -F: '{print $2}'|sed 's/,//'| sed 's/\"//g'` if [ $STATUS ] ; then exit else sleep 5 STATUS2=`curl -s --connect-timeout 2 --max-time 5 "Accept: application/json" "http://192.168.1.109:8080/json.htm?type=devices&rid=1" | grep "status"| awk -F: '{print $2}'|sed 's/,//'| sed 's/\"//g'` if [ $STATUS2] ; then exit else sleep 5 STATUS3=`curl -s --connect-timeout 2 --max-time 5 "Accept: application/json" "http://192.168.1.109:8080/json.htm?type=devices&rid=1" | grep "status"| awk -F: '{print $2}'|sed 's/,//'| sed 's/\"//g'` if [ $STATUS3 ] ; then exit else NOW=$(date +"%Y-%m-%d_%H%M%S") sudo service domoticz.sh stop sleep 8 sudo killall domoticz sleep 8 sudo service domoticz.sh start echo "$dt, Domoticz was offline. Restarted Domoticz...." >> "$CONFIG" fi fi fi
Code: Select all
Only when it tried to restart domoticz it will write a line in /home/pi/domoticz/scripts/bash/domoticz_state_checker.txtCarthman wrote: Thursday 19 December 2019 2:15 So I used this version for my pi. I can launch the script without errors, but is it not supposed to write in a log something ?
YesAnd finally you just have to add this script in the crontab ?
Is there a special need to have the log file under /scripts/bash, or can I put it just in /scripts ?waaren wrote: Thursday 19 December 2019 7:00Code: Select all
Only when it tried to restart domoticz it will write a line in /home/pi/domoticz/scripts/bash/domoticz_state_checker.txtCarthman wrote: Thursday 19 December 2019 2:15 So I used this version for my pi. I can launch the script without errors, but is it not supposed to write in a log something ?
You first need to create directory /home/pi/domoticz/scripts/bash
No you can put it anywhere you like. Of course you need to have enough access rights to write in the directory of choice.Carthman wrote: Thursday 19 December 2019 18:22 Is there a special need to have the log file under /scripts/bash, or can I put it just in /scripts ?
Code: Select all
#!/bin/bash
: '
crontab -e
# m h dom mon dow command
* * * * * /usr/bin/nice -n20 /home/pi/checkdomoticz.sh >/dev/null 2>&1
'
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
exit
else
sleep 20
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
exit
else
sleep 20
DOMOTICZ=`curl -s --connect-timeout 2 --max-time 5 "http://127.0.0.1:8080/json.htm?type=devices&rid=1"`
STATUS3=`echo $DOMOTICZ | jq -r '.status'`
if [ "$STATUS3" == "OK" ] ; then
exit
else
service domoticz stop
service domoticz start
#shutdown -r now
fi
fi
fiCode: Select all
apt install jqCode: Select all
chmod +x /home/pi/checkdomoticz.sh