I have a script that pings URLs and fetches files from selected URLs. If fetching or pinging to the respective URL fails for a specific no. of Times it updates a log file and forwards it to specific email addresses.

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.

#!/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 $@ } } } } } }
Fetching part is Similar and has no problems. Kindly Help. Thanx in advance

In reply to Ping through Cron Fails by Appy16

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.