< async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"">

Thursday, November 6, 2014

Automatically reboot raspberry pi media center (raspbmc) on internet loss with wifi dongle [headless raspberries on ssh]

ADVERTISEMENT
Raspberry pi stuck to a xbmc/kodi media center without keyboard

It is cool to keep a pi run as a media-center, attached to an lcd screen. But often we run it without a keyboard and rely on ssh or xbmc/kodi remotes on android/ios to control the media devices. It is possible that some of us use it to perform other internet of things and use the pi as a bridge to the internet. Hence it is important to keep it always connected to the internet.

Often it is seen that the pi loses the network connectivity when the  wifi network resets or some other strange reasons. A simple reboot will keep it live and hence accessible via ssh.

So i started using a simple script which reboots the pi when it cannot connect to the internet. It is set as a cron job so that it will be executed every 30 minutes so that the pi is kept online and hence accessible via ssh from a remote location.

So first ssh in to the pi and cd to home directory

ssh pi [email protected]
[email protected]:~$ cd ~


Now we keep our script in the home directory, so type  nano inetdown.sh and fill in the following


#!/bin/bash
COUNTER=0
while [ $COUNTER -lt 5 ] ## change ciunter if you need more rounds
do
if ping -c 1 8.8.8.8 &> /dev/null ## ping google for internet connectivity
then
exit 1
elif ping -c 1 8.8.4.4 &> /dev/null ##try again.
then
exit 1
else
let COUNTER=COUNTER+1
fi
done
#echo -n S > /dev/ttyUSB0  #i have an arduino clock which beeps to indicate the reboot
sudo /sbin/shutdown -r now
echo 0

and save it by pressing ctrl+X and save it at /home/pi/inetdown.sh

Now add this to a cronjob by

crontab -e 

and then add the following

*/40 * * * * /home/pi/inetdown.sh

as a last step, add this script to an excemption list to run as root (sudo) to allow reboot by typing

sudo visudo

and then append the following to the bottom of the file and save by ctrl+x

pi ALL=(ALL) NOPASSWD:/home/pi/inetdown.sh


end

No comments:

Post a Comment