erem wrote: ↑Sunday 11 April 2021 16:45
Here is my script to check if Domoticz is active, and restart it if not.
Hello,
Just take care that domoticz service may still be up, with event system still running (schedules/scripts always done...) but web server side down so no user interaction possible.
In fact, this is what happened most to me in the past!
So checking service up may not always do the job & you may send a json query like other suggestions, or use httping (after install) in a simple cron triggered script.
On top of that, when http server side is down, stopping/restarting service may no work: At restart, in some situations http port bind was not possible because still 'in-use". I then had to restart whole system...
In the end, as domoticz down whatever the reason will always mean http server down, I just check this and after a retry the next minute, if still down I 1st restart service, wait 1mn for http server being up and if still down, do a full shutdown/reboot after saving last domoticz log lines for post-mortem debug if needed.
The /root/checkDomoticz.sh file:
Code: Select all
#!/bin/bash
# Check domoticz (from a crontab) is up a restart whole PI if needed...
domoticzUrl=localhost:8080
BN=`basename $0`
WHOCOUNT=$(who | wc -l)
if [ ${WHOCOUNT} -ne 0 ]
then
logger $BN: Someone is logged on, no check.
exit 0
fi
httping -c 5 -i 0 -t 1 --ts -v -Wsqg $domoticzUrl
STATUS=$?
if [ ${STATUS} -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
STATUS=$?
if [ ${STATUS} -ne 0 ]
then
logger $BN: Still KO. Get last logs and try service restart then wait...
tail -n 20 /tmp/domoticz.txt | logger
service domoticz restart
sleep 1m
logger $BN: Check after service restart...
httping -c 5 -i 0 -t 1 --ts -v -Wsqg $domoticzUrl
STATUS=$?
if [ ${STATUS} -ne 0 ]
then
logger $BN: Still KO. Get last logs and REBOOT...
tail -n 20 /tmp/domoticz.txt | logger
/sbin/shutdown --no-wall -r now
STATUS=$?
else
logger $BN: Service restart OK !
fi
else
logger $BN: Retry OK !
fi
else
logger $BN: Domoticz ALIVE !
fi
wait
logger $BN: DONE, status= ${STATUS} !!!
exit ${STATUS}
This is called from a root cron job every 30mn, here's the crontab line:
Code: Select all
0,30 * * * * /root/checkDomoticz.sh > /dev/null 2>&1
For now, this never failed even if I have less issues than in the past (still using v4.10717 with a few web interface/JS fixes, by far the most stable version I had for now).
Just don't forget to stop the cron/rename script when intentionally stopping domoticz service: I screwed a raspbian version update in the middle of the process, with then a non-bootable system & a full reinstall needed!
EDIT :
Should add a apt lock check to this script, as I may forget about this when debian 11 will be out...
Added logged user check as suggested hereupper by Janni, that's much better!