Re: Script to restart Domoticz if it crashes
Posted: Sunday 02 July 2017 14:02
The script starts wit the /bin/bash shabang, no need to specify bash. Just execute it.
Open source Home Automation System
https://forum.domoticz.com/
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
Any suggestion?domoticz_state_checker.sh: 5: domoticz_state_checker.sh: [[: not found
domoticz_state_checker.sh: 8: domoticz_state_checker.sh: [[: not found
Hi Guys,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:With best regards UC530Code: Select all
crontab -e */1 * * * * /home/pi/domoticz/scripts/domoticz_state_checker.sh
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
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
ThanksEgregius wrote:apt-get install jq
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.');
}
}
Code: Select all
* * * * * /usr/bin/php /home/pi/checklastupdate.php
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