Last active
July 4, 2020 23:04
-
-
Save saper-2/35614f33d019865401f18dd8d437cd23 to your computer and use it in GitHub Desktop.
Script for testing if interface works ok and restart some services after interface recovers from failure.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# script for checking if specified interface is UP and properly connected to network (so ping can work) | |
# enter test ping address. IF ping fails it's marked by creation of file. When ping starts working again | |
# it restart few services (domoticz, mosquitto, samba) and docker containers (you need to specify your | |
# own containers IDs). | |
# script also write to syslog when the ping fail first time (when interface failed), and when recovered. | |
# crontab entry for running every 1min: | |
# * * * * * /root/restart-services.sh >/dev/null 2>&1 | |
# set to 1 to see more info (debug), not recommended when running from cron | |
DEBUG_MODE=0 | |
PING_ADDR=192.168.2.1 | |
PING_INTF=wlan0 | |
ERR_FILE=/tmp/ping-error | |
LOG_FILE=/var/log/svr-rst-netfail.log | |
ping -4 -I $PING_INTF -c 2 -q -W 1 $PING_ADDR | |
PING_ERR=$? | |
if [ $DEBUG_MODE -ne 0 ]; then | |
echo "PING result: $PING_ERR" | |
fi | |
if [ $PING_ERR -ne 0 ]; then | |
if [ -a $ERR_FILE ]; then | |
# do nothing | |
echo "Still interface down." | |
else | |
touch /tmp/ping-error | |
logger "Detected interface $PING_INTF failure, test ping failed." | |
echo "$(date -Iseconds) : Detected interface $PING_INTF failure, test ping failed." >> $LOG_FILE | |
if [ $DEBUG_MODE -ne 0 ]; then | |
echo "Logged error to syslog and created error-file: $ERR_FILE" | |
fi | |
fi | |
fi | |
#check if file exists indication last ping-error | |
if [ -a /tmp/ping-error ]; then | |
if [ $DEBUG_MODE -ne 0 ]; then | |
echo "Error file exists! Last ping failed." | |
fi | |
# check if ping above worked | |
if [ $PING_ERR -eq 0 ]; then | |
if [ $DEBUG_MODE -ne 0 ]; then | |
echo "Restarting services..." | |
fi | |
# if last ping-gateway was erro, but now it's ok | |
# restart some services | |
systemctl restart mosquitto | |
SRV_MOS=$? | |
systemctl restart domoticz | |
SRV_DOM=$? | |
systemctl restart smbd nmbd | |
SRV_SMB=$? | |
docker restart 3c060fc249a8 | |
DOC_R1=$? | |
docker restart 0377a1652133 | |
DOC_R2=$? | |
# delete file indication gateway ping-error | |
rm -f /tmp/ping-error | |
if [ $DEBUG_MODE -ne 0 ]; then | |
echo "Recovered from wifi failure, restart results: PING=$PING_ERR MOSQUITTO=$SRV_MOS DOMOTICZ=$SRV_DOM SMB=$SRV_SMB DOCKER=$DOC_R1 $DOC_R2" | |
fi | |
logger "Recovered from wifi failure, restart results: PING=$PING_ERR MOSQUITTO=$SRV_MOS DOMOTICZ=$SRV_DOM SMB=$SRV_SMB DOCKER=$DOC_R1 $DOC_R2" | |
echo "$(date -Iseconds) : Recovered from wifi failure, restart results: PING=$PING_ERR MOSQUITTO=$SRV_MOS DOMOTICZ=$SRV_DOM SMB=$SRV_SMB DOCKER=$DOC_R1 $DOC_R2" >> $LOG_FILE | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment