Last active
August 12, 2017 10:21
-
-
Save aravindkumarsvg/b73543b3640591974cb3a6e86c3c5e76 to your computer and use it in GitHub Desktop.
Automatically restart network manager in case of connection timeouts due to wifi problems
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 | |
############################################################# | |
# | |
# Restarts the Network Manager automatically when the | |
# Network disconnects, because of wifi connectivity problem | |
# | |
############################################################# | |
# Function which checks for the root user execution | |
root_checker() { | |
# Checks if the script is ran by the root user | |
if [ $EUID -ne 0 ] | |
then | |
echo -e "\nPlease run as root user!!!!!!!!!!!!!!!!!!!!!!!\n" | |
exit 1 | |
fi | |
} | |
# Exits the Program | |
die() { | |
echo -e "\nBye bye!!!!!!!!!!!!!!!\n" | |
exit 0 | |
} | |
# Traps the signal SIGINT | |
trap 'die' SIGINT | |
# Main flow of Execution | |
main() { | |
# Variable Declarations | |
local max_timeout_to_determine_conn_not_available=3 | |
local consecutive_timeouts=0 | |
# Ensures the script is executed with sudo persmission | |
root_checker | |
while : | |
do | |
ping -c3 google.com | |
if [ $? -ne 0 ] | |
then | |
consecutive_timeouts=`expr $consecutive_timeouts + 1` | |
if [ $consecutive_timeouts -gt $max_timeout_to_determine_conn_not_available ] | |
then | |
echo -e "\n\a\aConnection is not available!!!!!!!!!!!!!!!\n\a" | |
exit 1 | |
fi | |
systemctl restart network-manager | |
sleep 20 | |
else | |
consecutive_timeouts=0 | |
: | |
fi | |
done | |
} | |
# Starts the main flow | |
main | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment