FreeBSD Cron Job to run (ifconfig em0 down; ifconfig em0 up)

Hinkie Hinkie at paradise.net.nz
Fri Aug 31 18:55:09 PDT 2007


Dear People

As a courtesy to anyone interested I have finally sovled this (I hope), this is what I did, this is on a FreeBSD pfSense firewall router.

Essentially the fix is to ping the static IP's first hop, if this is down then flick the WAN NIC state down and up, this restores 
the lost connection where the Motorola 5101 has stopped sending packets (presumably for some incompatibility reason)  The Motorola 
5101 has today been replaced with a 5100, the ISP tell me most commercial lines are running the 5100 as they say it is more router 
compatible than the newer 5101.  I'll advise if the 5100 exhibits the same behaviour(!) however if it does the following should 
address it within a minute.  If you are copying it be sure to copy it exactly as spaces in the wrong place stuff it up....etc!!

For both the lists and my record it is done by:

=> in /etc/crontab add
*/1 * * * * root /usr/bin/pinger.sh

=> from edit.php create / write into new file /usr/bin/pinger.sh
#!/bin/sh

ping -c1 Insert_1st_Gateway_Hop_Here_commonly_Static_IP_a.b.c.1
if [ $? -eq 2 ]; then
        ifconfig em0 down
        ifconfig em0 up
        echo 'Gateway Down'
else
        echo 'Gateway Up'
fi

=> from exec.php run chmod u+x /usr/bin/pinger.sh

=> from exec.php run ls -l /usr/bin/pinger.sh
and check there is an x in the file permissions (for executable)

It will have run when you see a log series of commands starting with
Sep 1 11:32:13 kernel: em0: link state changed to UP
Sep 1 11:32:11 kernel: em0: link state changed to DOWN

The only problem I see with this approach is that whenever the Internet is down for whatever reason the WAN interface is going to be 
disconnected and reconnected every minute, as well as filling the logs with this info, but that seems only of concern from the 
perspective of filling the log with rubbish.  I might tinker with it to send me an email to advise me when the code has also run .

Thank you again to the people who worked with me on this.

Kind regards
David Hingston 


More information about the freebsd-questions mailing list