Appy16 has asked for the wisdom of the Perl Monks concerning the following question:
The Script reads The Urls, the function ( ping or fetch ), the no. of times the function has to be carried out before sending a failure email and the email addresses from a specific text file.
To automate the process i am using cron which runs the script every 30 minutes.The problem is that a few URLs are not pinged when the program is run through the cron tab command and their failure is reported, but if I run the program manually i can ping those URLs.
Is there any way to find the reason for the failure during execution of the program eg. if the error is Request Time out or something like that then it gets added to the log file as the reason.I am posting my script below.
Fetching part is Similar and has no problems. Kindly Help. Thanx in advance#!/usr/bin/perl use warnings; use LWP::Simple; use Email::Send; use Email::Send::Gmail; use Email::MIME::Creator; use Net::Ping::External qw(ping); use File::Fetch; use Tie::File; my $testfolder = "/Users/Appy/Desktop/"; my $to = "test2\@gmail.com"; my $from = "test1\@gmail.com"; my $date = localtime(); my $count = 0; my $logfilelocation = "/Users/Appy/Desktop/"; tie @file, 'Tie::File', $testfolder . "testfile.txt" or die; foreach $URL (@file) { my $string1 = $URL; if ( $string1 =~ m/ # Match ping\s # 'ping ' /ix # i = Ignore case # x = allow the regexp to go over multiple + lines ) { my $URI = substr $URL, 8, 28; print "$URI\n"; &myping($URI,$URL); sub myping { my $newURI = $_[0]; my $newURL = $_[1]; my $alive = ping(host => "$newURI"); if ($alive) { print "$newURI is active.\n"; $count = 0; } else { print "$newURI is inactive\n"; my $c = substr $newURL, 5, 2; if ($count<$c) { $count = $count + 1; print "$count\n"; &myping; } elsif ($count==$c) { my $logfile = $logfilelocation . "log.txt"; my $logmsg = "$date cannot ping $newURI"; open LOGFILE, ">>$logfile" or die "cannot open $logfile for append: $! +"; print LOGFILE $logmsg, "\n"; close LOGFILE; my $emailadd = substr $newURL, 22; @personal = split(/,/, $emailadd); foreach $eadd (@personal) { my $email = Email::MIME->create( header => [ From => $from, To => $eadd, Subject => $logmsg, ], attributes => { filename =>"log.txt", content_type =>"application/text", disposition =>"attachment", Name =>"/Desktop/log.txt", }, body => "$newURI is not working" ); my $sender = Email::Send->new( { mailer => 'Gmail', mailer_args => [ username => 'appy.test1@gmail.com', password => '1234qwerty', ] } ); eval { $sender->send($email) }; die "Error sending email: $@" if $@ } } } } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Ping through Cron Fails
by graff (Chancellor) on Mar 30, 2010 at 13:28 UTC | |
|
Re: Ping through Cron Fails
by Damashii (Scribe) on Mar 30, 2010 at 13:29 UTC |