- Put the script into the home dir, like: /home/pi
- Edit the parameter in the script
vim NetReconnector.sh
NetCard and PingTarget should be modified.
- Add x priority to the script
chmod +x NetReconnector.sh
- Add a task with crontab
crontab -e
Add this line which means run the script every 5 minutes between 8:00 and 22:00.
*/5 8-22 * * * ~/NetReconnector.sh
Log file generated in home dir, like: /home/pi/NetReconnector.log
#!/bin/bash
# by nickwang wxlcat@gmail.com
NetCard=wlan0 # the netcard connected to the internet
PingTarget=www.baidu.com # the ping target, router ip or website, etc: 192.168.1.1, www.baidu.com
LogFile=~/NetReconnector.log # log file
LogAll=false # log msg no matter whether the net is connected, used to be check whether the script is running.
test -e $LogFile || touch $LogFile
time=$(date "+%Y/%m/%d %H:%M:%S")
ret=$(ping -c 2 -W 1 -I $NetCard $PingTarget | grep 'received' | cut -d ',' -f 2 | cut -d ' ' -f 2)
if [ "$ret" == "0" ]; then
sudo ifdown wlan0 && sudo ifup wlan0
echo "Try Reconnect: $?, $time" | tee -a $LogFile
else
if [ "$LogAll" == "true" ]; then
echo "Network is ok, ret = $ret, $time" | tee -a $LogFile
fi
fi
exit 0
Source: Github
PD: Could be good if you add this script to DietPi because Wifi sometimes drop connection and with this seems to be working 24/7.
I only tested this 2 days and I had no drops, I will write later if my connection drops.