[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: ping script



On Fri, Jan 16, 2004 at 11:26:11PM -0800, Curtis Vaughan wrote:
> Actually this is the way I tried to get it to continually loop. 
> Apparently it does, but it only send out messages as to failures after 
> I have killed the process.
> 
> #!/bin/bash
> while :; do
> ping='10.0.0.3'
> if ping -c 1 $ping;
> then echo "success";
> else echo "ping failed for $ping" | mail -s "ping failure for $ping" 
> curtis;
> fi
> done

I think that it might be succeeding, and therefore not sending any mails,
but when you kill it you don't kill the script but just the ping command
causing it to "fail". That's why you get the emails when you kill it.

Here's a perl script that repeats the ping every ten seconds until it fails
then it stops running:
#!/usr/bin/perl
$ping='10.0.0.3';
while(!$fail)			#check if last ping failed
{
	`ping -c 1 $ping`;	#execute the ping
	if($?==0)		#check the status of last command
	{
		print "success\n";
	}
	else
	{
		`echo "ping failed for $ping" | mail -s "ping failure for $ping" curtis`;
		$fail=true;
	}
	sleep(10);
}

If you want it to go on forever (even after the ping starts to fail) simply
change the loop from while(!$fail) to while(1).

Bijan
-- 
Bijan Soleymani <bijan@psq.com>
http://www.crasseux.com

Attachment: signature.asc
Description: Digital signature


Reply to: