mcgyver83
Posts: 365
Joined: Fri Oct 05, 2012 11:49 am

Auto reboot if connection lost

Tue Oct 16, 2012 9:40 am

I'm thinking about a script that will ping google.com every 10 minutes and if it fails reboot the raspberry.
Some suggestion?

User avatar
malakai
Posts: 1382
Joined: Sat Sep 15, 2012 10:35 am

Re: Auto reboot if connection lost

Tue Oct 16, 2012 10:25 am

Not sure if your asking how or if it's a good idea (there are no bad ideas really - just one's that end in infinite loops) :)
For those not sure how this is the start. Other than that why not reboot if you can.

cron is a time-based scheduling service in Linux

Type the following command to enter cronjob:
$ crontab -e
Each cronjob has following syntax:

minute (0-59), hour(0-23), day of month, month, day of week(Sunday=0 Saturday=6 i think 7 is everyday)

so for today at 3:50 pm run once
50 3 16 10 2

* * * * * command to be executed

so every 10 minutes:
*/10 * * * * /path/to/ping.sh for example

Then write the script
http://www.raspians.com - always looking for content feel free to ask to have it posted. Or sign up and message me to become a contributor to the site. Raspians is not affiliated with the Raspberry Pi Foundation. (RPi's + You = Raspians)

mcgyver83
Posts: 365
Joined: Fri Oct 05, 2012 11:49 am

Re: Auto reboot if connection lost

Tue Oct 16, 2012 10:42 am

I'm asking how to do it :)
I know crontab, I'm not able to write a script that check a url and reboot if lan is disconnected.
Connection lost is the only problem I had until now and with this script I can avoid manual remove/plug power :-)

ski522
Posts: 394
Joined: Sun Sep 30, 2012 2:22 pm

Re: Auto reboot if connection lost

Tue Oct 16, 2012 10:58 am

Linux machines rarely need rebooting and it would be odd for the ethernet interface to go down for no reason. Why do you feel the need to reboot, are you having problems with your ethernet interface going down? That would point to a different problem. You should check the /var/log/syslog file next time you lose connectivity to see what might be happening.

As for the a crontab job, I believe all you have to do is run the following command "/etc/init.d/networking restart"...maybe follow it up with "dhclient eth0" to ensure you dhcp address is pulled.

mcgyver83
Posts: 365
Joined: Fri Oct 05, 2012 11:49 am

Re: Auto reboot if connection lost

Tue Oct 16, 2012 12:18 pm

I know that linux machines usually don't need reboot.
I'm waiting a power supply and in the meanwhile I'm using a phone 220V->usb adapter that provide 500mA.
If I put the rasp under heavy load it will cut the power to the lan adapter to save the cpu work; in this situation (I'm in headless system) the only thing I can do is unplug/plug the power.
With this script the raspberry will reboot automatically preventing data corruption that could happen unpluging the power manually.

After that I want to learn how to do this things with linux.

ski522
Posts: 394
Joined: Sun Sep 30, 2012 2:22 pm

Re: Auto reboot if connection lost

Tue Oct 16, 2012 4:11 pm

Haven't tried it myself on the PI, but you can use the watchdog app in linux that should be able to do what you're looking for without needing to write an app or create a cron job.

http://linux.die.net/man/8/watchdog

Code: Select all

sudo apt-get install watchdog

mcgyver83
Posts: 365
Joined: Fri Oct 05, 2012 11:49 am

Re: Auto reboot if connection lost

Mon Oct 29, 2012 12:26 am

As I wrote here http://www.raspberrypi.org/phpBB3/viewt ... 73#p199673 I tried to use watchdog but I cannot find how to configure the ip ping option...

User avatar
malakai
Posts: 1382
Joined: Sat Sep 15, 2012 10:35 am

Re: Auto reboot if connection lost

Mon Oct 29, 2012 3:45 am

I think it's just add ping in the watchdog.conf file

ping = <ip-addr>
Set IP address for ping mode. This option can be used more than once to check different connections.

http://swoolley.org/man.cgi/watchdog

A possible setting might be this:
ping = <ip to ping>
interface = eth0
interval = <time in seconds>

Can't find a ton of information on how to configure the interval hope it helps.
http://www.raspians.com - always looking for content feel free to ask to have it posted. Or sign up and message me to become a contributor to the site. Raspians is not affiliated with the Raspberry Pi Foundation. (RPi's + You = Raspians)

mcgyver83
Posts: 365
Joined: Fri Oct 05, 2012 11:49 am

Re: Auto reboot if connection lost

Mon Oct 29, 2012 11:02 am

I will try as soon as the rpi ends the XBMC compile.
Thanks

ski522
Posts: 394
Joined: Sun Sep 30, 2012 2:22 pm

Re: Auto reboot if connection lost

Mon Oct 29, 2012 11:22 am

I tried adding watchdog last week, but something is broken in watchdog (On the PI only) where ping doesn't work, so you end up with a constant reboot because watchdog starts up at boot time. If you want to experiment with watchdog first edit

Code: Select all

sudo nano /etc/default/watchdog
and tell watchdog not to start at boot. I haven't tried watchdog with the latest firmware, so not sure if the issue has been fixed, but doubt it. I tried compiling watchdog source and still had problems, so I'm guessing PI has a problem with one of it's library files that needs to be fixed.

mcgyver83
Posts: 365
Joined: Fri Oct 05, 2012 11:49 am

Re: Auto reboot if connection lost

Mon Oct 29, 2012 11:26 am

Excellent.I'm writing this exact issue: when I enable in watchdog.conf the ping test it starts an endless reboot cycle.
So it's not my fault :-) I'm happy...sometimes I don't crash all I touch :-)

mcgyver83
Posts: 365
Joined: Fri Oct 05, 2012 11:49 am

Re: Auto reboot if connection lost

Mon Dec 03, 2012 3:39 pm

So watchdog + ping doesn't work fine or someone found a fix to use this nice feature?

poing
Posts: 1132
Joined: Thu Mar 08, 2012 3:32 pm

Re: Auto reboot if connection lost

Mon Dec 03, 2012 3:47 pm

I'm also interested in this, although for a different reason. I have an embedded Pi and when home I want to plug it into my LAN and automagically start uploading files to a remote server. So I need to establish if the remote server is pingable ( :D ) and then launch a script - the reverse of what the OP wants.

sdjf
Posts: 1397
Joined: Fri Mar 16, 2012 5:20 am
Location: California

Re: Auto reboot if connection lost

Mon Dec 03, 2012 5:06 pm

Here is how I would handle this, using a dialup connection. If your connection is via usbd0 or eth0 or wlan0 or whatever, you would substitute that for ppp0 in the following:

#!/bin/bash

RESCHEDULE THE NEXT CRON JOB WITH CODE HERE

if ifconfig ppp0 > /dev/null ; then

CODE ACTION TO TAKE IF PI IS CONNECTED

else

CODE ACTION TO TAKE IF PI IS *NOT* CONNECTED

fi

exit

I sent the output to /dev/null as I only want to test the connection, without getting the ifconfig output. If you want to see the ifconfig output, then just remove the "> /dev/null"

I put the code for rescheduling the cron job before both forks of the script because if you reboot, I think the script would never finish. And it needs rescheduling anyhow.

I also cannot give you better information about the syntax for rescheduling cron as I use "at" jobs in my other systems - Pi is on it's way to me, not here yet.

I suppose you could try something similar with ping, adding more parameters to limit how many tried and then grepping for a specific word in the output that would only appear if pinging was successful. Might not need to grep, but the if COMMAND;then COMMAND;else COMMAND;fi is a pretty handy approach sometimes.

Those reading this post who have no experience with writing bash scripts, might want to read my very basic beginner's page about writing short shell scripts.

http://sdjf.esmartdesign.com/scripts/shortsh.html
FORUM TIP: To view someone's posting history, sign in, click on their user name, then on "Search User's Posts." || Running ArchLinuxArm on Model 2B and 512MB Model B

poing
Posts: 1132
Joined: Thu Mar 08, 2012 3:32 pm

Re: Auto reboot if connection lost

Mon Dec 03, 2012 5:13 pm

Thanks, also for the tutorial on shell scripts!

sdjf
Posts: 1397
Joined: Fri Mar 16, 2012 5:20 am
Location: California

Re: Auto reboot if connection lost

Mon Dec 03, 2012 5:58 pm

@poing: thank you! I have to wait for my Pi to arrive to cook up some more simple examples specifically useful for a Pi, but did my best to go into detail so anybody could follow the explanations.
FORUM TIP: To view someone's posting history, sign in, click on their user name, then on "Search User's Posts." || Running ArchLinuxArm on Model 2B and 512MB Model B

ski522
Posts: 394
Joined: Sun Sep 30, 2012 2:22 pm

Re: Auto reboot if connection lost

Mon Dec 03, 2012 6:57 pm

I got this from another post here on the forum, but don't recall the post itself...so if you want a script that will reboot the PI on connection lost first:

sudo mkdir /opt/check_lan
sudo nano /opt/check_lan.sh

Then enter the follow bash script:

Code: Select all

#!/bin/sh

# cron script for checking wlan connectivity
# change 192.168.1.1 to whatever IP you want to check.
IP_FOR_TEST="192.168.1.1"
PING_COUNT=1

PING="/bin/ping"
IFUP="/sbin/ifup"
IFDOWN="/sbin/ifdown --force"

INTERFACE="eth0"

FFLAG="/opt/check_lan/stuck.fflg"

# ping test
$PING -c $PING_COUNT $IP_FOR_TEST > /dev/null 2> /dev/null
if [ $? -ge 1 ]
then
    logger "$INTERFACE seems to be down, trying to bring it up..."
        if [ -e $FFLAG ]
        then
                logger "$INTERFACE is still down, REBOOT to recover ..."
                rm -f $FFLAG 2>/dev/null
                sudo reboot
        else
                touch $FFLAG
                logger $(sudo $IFDOWN $INTERFACE)
                sleep 10
                logger $(sudo $IFUP $INTERFACE)
        fi
else
#    logger "$INTERFACE is up"
    rm -f $FFLAG 2>/dev/null
fi
Then in a cron job you can run this script every 2 minutes.
sudo nano /etc/crontab and enter the following at the bottom:

Code: Select all

*/2 * * * *    root    /opt/check_lan/lan.sh >/dev/null

tobie
Posts: 11
Joined: Tue May 15, 2012 5:15 pm

Re: Auto reboot if connection lost

Wed Dec 19, 2012 12:44 pm

hi, have you solved this one OP?

I've set up a cron to ping multiple hosts using this walk-through:

http://www.cyberciti.biz/tips/simple-li ... ripts.html

This helped me understand the ping command, how to use grep and awk to extract relevant detail from the output of a command, how to write a shell script, and how to run a shell script from cron

schneidz
Posts: 22
Joined: Sat Sep 15, 2012 9:08 pm

Re: Auto reboot if connection lost

Sun Dec 23, 2012 7:54 pm

maybe put this in /etc/rc.d/rc.local:

Code: Select all

while [ 1 ]
do
 if [ -z "$(ping -c 1 www.02144.com)" ]
 then
  shutdown -r now
  #ifconfig eth0 down && ifconfig eth0 up
  #/etc/init.d/networking restart
  #dhclient eth0
 fi
 sleep 600
done
the commented part is probably all that you need

mcgyver83
Posts: 365
Joined: Fri Oct 05, 2012 11:49 am

Re: Auto reboot if connection lost

Fri Dec 28, 2012 9:33 am

I'm trying with the Ski522 solution, further I'll try the other.

Thanks

efflandt
Posts: 359
Joined: Mon Dec 03, 2012 2:47 am
Location: Elgin, IL USA

Re: Auto reboot if connection lost

Sat Dec 29, 2012 7:39 am

No real need to generate internet traffic pinging, and if you keep it up, the site might block you. Just ping your default gateway (router).

But after doing rpi-update, I no longer have problems with ethernet tossing errors or choking USB on any of my 3 Pi. So maybe doing rpi-update (or when they incorporate those fixes into current distro) would make this discussion moot. https://github.com/Hexxeh/rpi-update/

cabbers
Posts: 50
Joined: Sat Nov 17, 2012 10:58 am

Re: Auto reboot if connection lost

Sat Jan 12, 2013 11:05 am

ski522 wrote:I got this from another post here on the forum, but don't recall the post itself...so if you want a script that will reboot the PI on connection lost first:

sudo mkdir /opt/check_lan
sudo nano /opt/check_lan.sh

Then enter the follow bash script:

Code: Select all

#!/bin/sh

# cron script for checking wlan connectivity
# change 192.168.1.1 to whatever IP you want to check.
IP_FOR_TEST="192.168.1.1"
PING_COUNT=1

PING="/bin/ping"
IFUP="/sbin/ifup"
IFDOWN="/sbin/ifdown --force"

INTERFACE="eth0"

FFLAG="/opt/check_lan/stuck.fflg"

# ping test
$PING -c $PING_COUNT $IP_FOR_TEST > /dev/null 2> /dev/null
if [ $? -ge 1 ]
then
    logger "$INTERFACE seems to be down, trying to bring it up..."
        if [ -e $FFLAG ]
        then
                logger "$INTERFACE is still down, REBOOT to recover ..."
                rm -f $FFLAG 2>/dev/null
                sudo reboot
        else
                touch $FFLAG
                logger $(sudo $IFDOWN $INTERFACE)
                sleep 10
                logger $(sudo $IFUP $INTERFACE)
        fi
else
#    logger "$INTERFACE is up"
    rm -f $FFLAG 2>/dev/null
fi
Then in a cron job you can run this script every 2 minutes.
sudo nano /etc/crontab and enter the following at the bottom:

Code: Select all

*/2 * * * *    root    /opt/check_lan/lan.sh >/dev/null
Hi there-this is what I'm looking for my Pi to do-the PI sometimes is locking up, and I think its a DHCP issue on the host, but I'd like the Pi to reboot, and force a new DHCP lease to test. However, I've followed the steps above, and the Pi wont reboot. I think there are a few things wrong, which hopefully someone can confirm?

1) You need to make the check_lan.sh script executable by using the chmod command-so
sudo chmod 700 /opt/check_lan.sh
2) When putting this in as a cron job the command should be */2 * * * * root /opt/check_lan.sh >/dev/null ,not */2 * * * * root /opt/check_lan/lan.sh >/dev/null

I also changed the IP to bbc.co.uk. It all seems to work now. I can see the eth0 resetting, and then I think, about 2 minutes later, the Pi will do a full reboot.

Thanks

cabs

cabbers
Posts: 50
Joined: Sat Nov 17, 2012 10:58 am

Re: Auto reboot if connection lost

Sat Jan 12, 2013 11:40 am

Update on the above-with bbc.co.uk, the Pi reboots after 10 seconds.Seems to take about 2 minutes if I used my default gateway (ADSL router)

cheers

cabs

cabbers
Posts: 50
Joined: Sat Nov 17, 2012 10:58 am

Re: Auto reboot if connection lost

Sat Aug 31, 2013 5:49 pm

Hi all
I have a further question on this-I'm using the Hamachi VPN clinet on my Pi's, and it works pretty well, but on occasions the Hamachi interface (ham0)/hamachi client on the Pi "hangs".So the Pi can still ping bbc.co.uk (set up in cron to do this every 5 minutes) but I lose site of the Pi (the Pi's are in remote locations). Its very frustrating so what I am going to do is set up two Pi's as hosts, both in secure, but different locations (home and work!).
So what I want to do is change the IP I'm pinging in the script from bbc.co.uk to my two new host Pi's

# cron script for checking Internetconnectivity
# change 192.168.1.1 to whatever IP you want to check.
IP_FOR_TEST="hamachi Pi ip address"
PING_COUNT=1

in the seperate, but different,locations.As you can see I have one IP in the script at the moment-how do I add a second host in the script? So the Pi would need to see BOTH IP's as unreachable (I'm going for a belt and braces approach!), before it first takes down the ham0 interface, and then reboots the Pi

cheers

cabs

schneidz
Posts: 22
Joined: Sat Sep 15, 2012 9:08 pm

Re: Auto reboot if connection lost

Tue Dec 03, 2013 5:45 pm

^ not exactly sure what you are trying to achieve (and why) but heres my stab at it:

Code: Select all

while [ 1 ]
do
 if [ "$(ping -c 1 www.02144.com | grep '100% packet loss')"  -a "$(ping -c 1 www.something.com | grep '100% packet loss')" ]
 then
  #sudo shutdown -r now
  sudo ifconfig eth0 down && sudo ifconfig eth0 up
  sudo /etc/init.d/networking restart
  sudo dhclient eth0
 fi
 sleep 600
done

Return to “General discussion”