Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

So I want to keep an eye on my network connectivity so I can make a report for my ISP. (seems like the modem goes out for about 30-60 seconds about once a day)

So I copied this handy little script

http://www.cyberciti.biz/tips/simple-linux-and-unix-system-monitoring-with-ping-command-and-scripts.html

And an running it with

watch -n 15 ./ping-test.sh

I have been testing it by pulling the plug on my modem.

The first time watch runs the script after the network goes out it runs fine (sends some email) and then goes defunct. Once networking is restored the script "finishes" and then watch runs it again.

So I'm getting mail for when the network goes down, but not how long it was down for.

If I run the script straight from the command line it runs (emails) and then exits.

Any ideas why? How can I get the script to keep running once the network is down?

share|improve this question
add comment

4 Answers

up vote 1 down vote accepted

ping will hang if it cannot reach its target and that maybe why the script goes defunct because the ping is still waiting for a reply. Try adding the -w deadline or -W timeout option to the ping.

share|improve this answer
    
Sorry for taking so long to mark this correct. This was the trick I needed. I set -W 2 and now it works like a champ! –  Slashterix Jun 19 '12 at 3:47
add comment

You can do this in realtime by using "ip monitor". This will output any events such as the ip address being removed or added the link state changing from operational to disconnected, or a route changing in some way.

ip monitor all >> /some/log.log &

You must open in append mode. If you want to leave it to run outside of the terminal you can just disown the job with "disown %1"

If you need timestamps too, theres a couple of ways. An easy way is to use inotifywait to check if the file gets an update, then append your date to it. Itd be easy to do the above and this into a shell script and perhaps disown that instead so you get

ip monitor all >> /some/log.log &
while inotifywait -qq -e modify /some/log.log; do 
  sleep 0.2;
  date >>/root/mon.log; 
done

Enjoy

share|improve this answer
    
Cool tool. But I think it only monitors local connectivity. My modem (on the other side of a router) is going down. So the system I am testing from still has an active link to the LAN, just not the internet. –  Slashterix Nov 19 '11 at 1:17
add comment

Your solution with 'watch' command is not good. I would try to solve this with something like this:

#!/bin/bash
HOSTS="foo.bar foo2.bar"
COUNT=4
while :
do
    for myHost in $HOSTS
    do
      count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
      if [ $count -eq 0 ]; then
        # 100% failed 
        echo "Host : $myHost is down (ping failed) at $(date)"
      fi
      done
sleep 15 # here is your refresh rate
done

and then run it as normal script: ./script_name.sh

terminate with CTRL+C.

share|improve this answer
add comment

It goes defunct because it is already finished but depends on a parent process - watch command which is still alive.

So I'm getting mail for when the network goes down, but not how long it was down for.

That script is already print the date when the remote system is down:

echo "Host : $myHost is down (ping failed) at $(date)" | mail -s "$SUBJECT" $EMAILID

How can I get the script to keep running once the network is down?

It is pinging with -c 1 so it will stop after sending 1 ECHO_REQUEST packet.

Put it in cron to run every minutes:

*/1 * * * * /path/to/your/script
share|improve this answer
    
When the ping is succeeding, watch will keep re-running the script every 15s. I don't want to use cron as 1 minute is not a small enough window of time. I think the modem is dropping off for 30-60 seconds at a time. I want to get an email every 15s until the link is restored, so I can tell if it was down for 30s or 2 minutes. –  Slashterix Nov 19 '11 at 1:14
    
How about this while true; do /path/to/your/script; sleep 15; done? –  quanta Nov 19 '11 at 16:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.