Acho que a solução hololeap está funcionando.
Minha solução verifica cada N minutos (dependendo de como você configura seu crontab) para uma conexão de rede ativa.
Se a verificação falhar, acompanho a falha.
Quando a contagem de falhas é > 5 Eu tento reiniciar wifi (você também pode reiniciar o Raspberry se o wifi falhar, verifique os comentários).
Aqui está um repositório do GitHub contendo sempre a versão mais recente do script:
link
Aqui, de acordo com a política geral do stackexchange (todas as respostas não devem conter apenas links), também o arquivo network_check.sh, copie e cole em qualquer pasta que desejar, instalando instruções nos comentários do script.
#!/bin/bash
# Author:
# twitter.com/pitto
#
# HOW TO INSTALL:
#
# 1) Install ifupdown and fping with the following command:
# sudo apt-get install ifupdown fping
#
# 2) Then install this script into a folder and add to your crontab -e this row:
# */5 * * * * /yourhome/yourname/network_check.sh
#
# Note:
# If you want to perform automatic repair fsck at reboot
# remember to uncomment fsck autorepair here: nano /etc/default/rcS
# Let's clear the screen
clear
# Write here the gateway you want to check to declare network working or not
gateway_ip='www.google.com'
# Here we initialize the check counter to zero
network_check_tries=0
# Here we specify the maximum number of failed checks
network_check_threshold=5
# This function will be called when network_check_tries is equal or greather than network_check_threshold
function restart_wlan0 {
# If network test failed more than $network_check_threshold
echo "Network was not working for the previous $network_check_tries checks."
# We restart wlan0
echo "Restarting wlan0"
/sbin/ifdown 'wlan0'
sleep 5
/sbin/ifup --force 'wlan0'
sleep 60
# If network is still down after recovery and you want to force a reboot simply uncomment following 4 rows
#host_status=$(fping $gateway_ip)
#if [[ $host_status != *"alive"* ]]; then
# reboot
#fi
}
# This loop will run network_check_tries times and if we have network_check_threshold failures
# we declare network as not working and we restart wlan0
while [ $network_check_tries -lt $network_check_threshold ]; do
# We check if ping to gateway is working and perform the ok / ko actions
host_status=$(fping $gateway_ip)
# Increase network_check_tries by 1 unit
network_check_tries=$[$network_check_tries+1]
# If network is working
if [[ $host_status == *"alive"* ]]; then
# We print positive feedback and quit
echo "Network is working correctly" && exit 0
else
# If network is down print negative feedback and continue
echo "Network is down, failed check number $network_check_tries of $network_check_threshold"
fi
# If we hit the threshold we restart wlan0
if [ $network_check_tries -ge $network_check_threshold ]; then
restart_wlan0
fi
# Let's wait a bit between every check
sleep 5 # Increase this value if you prefer longer time delta between checks
done
edite em 1/26/2018:
Eu removi os arquivos temporários para deixar o script rodar na memória e evitar escrever no cartão SD do Raspberry.