tags:

views:

2409

answers:

5

Good day! Is there any way to include a timer (timestamp?or whatever term it is) in a script using bash? Like for instance; every 60 seconds, a specific function checks if the internet is down, if it is, then it connects to the wifi device instead and vice versa. In short, the program checks the internet connection from time to time.

Any suggestions/answers will be much appreciated. =)

+4  A: 

You may want to do a man cron.

Or if you just have to stick to bash just put the function call inside a loop and sleep 60 inside the iteration.

Alberto Zaccagni
+11  A: 

Blunt version

while sleep 60; do
  if ! check_internet; then
    if is_wifi; then
       set_wired
    else
       set_wifi
    fi
  fi
done

Using the sleep itself as loop condition allows you to break out of the loop by killing the sleep (i.e. if it's a foreground process, ctrl-c will do).

If we're talking minutes or hours intervals, cron will probably do a better job, as Montecristo pointed out.

roe
I see.. I'll try this one. Thank you guys! I better read bout "man cron" too then. =)
Suezy
function calls should not have parens at the end.
camh
absolutely correct, only the declaration should, i'll change that...
roe
A: 

Create a bash script that checks once if internet connection is down and add the script in a crontab task that runs every 60 seconds.

tricat
+4  A: 

Please find here a script that you can use, first add an entry to your cron job like this:

$ sudo crontab -e * * * * * /path/to/your/switcher

This is simple method that reside on pinging an alive server continuously every minute, if the server is not reachable, it will switch to the second router defined bellow.

surely there are better way, to exploit this issue.

$ cat > switcher

#!/bin/sh

route=`which route`
ip=`which ip`

# define your email here
mail="[email protected]"

# We define our pingable target like 'yahoo' or whatever, note that the host have to be 
# reachable every time
target="www.yahoo.com"

# log file
file="/var/log/updown.log"

# your routers here
router1="192.168.0.1"
router2="192.168.0.254"

# default router
default=$($ip route | awk '/default/ { print $3 }')

# ping command
ping -c 2 ${target}

if [ $? -eq 0 ]; then
   echo "`date +%Y%m%d-%H:%M:%S`: up" >> ${file}

else
   echo "`date +%Y%m%d-%H:%M:%S`: down" >> ${file}

   if [ ${default}==${router1} ]; then
       ${route} del default gw ${router1}
       ${route} add default gw ${router2}
   elif [ ${default}==${router2} ];  then
       ${route} del default gw ${router2}
       ${route} add default gw ${router1}
   fi
   # sending a notification by mail or may be by sms
   echo "Connection problem" |mail -s "Changing Routing table" ${mail}
fi
mezgani
typo above - should be "sudo crontab -e ..." not "sudo contab -e ..."
JJ
This looks like the best solution as cron is designed for this sort of task. Do something (ie a script) over some repeatable time delay.
Jim
I think that the script miss to detect automatically the default router, but think that i found it
mezgani
I really think one has to teach how to fish, the final use of this is CTRL+C CTRL+V... imho ^^
Alberto Zaccagni
I did not test it but i think, that it will work :)
mezgani
A: 
William Pursell