niedziela, 9 lutego 2014

Ethernet connection drop on Raspberry Pi

From time to time internet connection to my Raspberry Pi disappears (AFAIK it is well known feature of Pi). Removing and reconnecting the ethernet cables did not work, so one has to reboot by pulling the plug (risking to corrupt file systems from improper shutdown).

To reconnect I use the following bash script (found here):


#!/bin/bash
# Check if Internet connection is still alive ***
# Insert into crontab at *.16
LOGLOG="/home/pi/Logs/Cron/Reboot.log"
WLAN=eth0

if /sbin/ifconfig $WLAN | grep -q "inet addr:" ; then
echo "Network connection up!"
else
echo "Attempting reconnect [`date +%Y%m%d%H%M`]" >> $LOGLOG
/usr/bin/sudo /sbin/ifup --force $WLAN
fi

Note: use ifconfig -a to identify relevant interface (WLAN):


pi@raspberryberrystar ~ $ ifconfig -a ifconfig -a
eth0 Link encap:Ethernet HWaddr b8:27:eb:f5:9a:d7
inet addr:192.168.1.146 Bcast:192.168.1.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

My other Pi is connected to internet not through a cable but with WiFi dongle. The relevant interface is:


pi@blackberrystar ~ $ ifconfig -a
wlan2 Link encap:Ethernet HWaddr 10:fe:ed:12:ef:2c
inet addr:192.168.1.144 Bcast:192.168.1.255 Mask:255.255.255.0

The connection is checked every 20 minutes with the following crontab entry:


## Check Internet connection every 7th, 27th, 47th minute:
7,27,47 * * * * /home/pi/bin/chk_www_alive.sh > /dev/null 2>&1