Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

How to send the "die" output to e-mail.

by pmu (Beadle)
on Nov 20, 2013 at 18:44 UTC ( #1063573=perlquestion: print w/ replies, xml ) Need Help??
pmu has asked for the wisdom of the Perl Monks concerning the following question:

Respected Monks,

I have written a script that logs into some storage arrays and sends out an e-mail which has a text file attachment containing the monitoring/health check outputs. At times, the script cannot completely run because it cannot access the storage arrays (when there are network issues) and when run from the command prompt in such situations, it will promptly "die". I am looking for ways to capture the "die" output in a mail so that my team would know something went wrong. As of now, either it sends the report or in case of network issues wherein it cannot access the storage arrays, the script just dies, and no mail is sent.

I am using MIME::Lite to get the e-mailing of the reports done and it works right. But is there a way to send the output of "die" to a file/e-mail?

|| Aeterna Est Perspectum Cognitio ||

Comment on How to send the "die" output to e-mail.
Re: How to send the "die" output to e-mail.
by hippo (Deacon) on Nov 20, 2013 at 18:52 UTC

    This is called "exception handling" (among other names). The modern way of performing this is to use any of the "Try" modules - I find that Try::Tiny is a good place to start. Older code might rely more heavily on eval which is not without its problems, hence the "Try" modules.

    If you are familiar with exception handling from other languages where it is built in, you should have no problems with it in Perl.

Re: How to send the "die" output to e-mail.
by boftx (Chaplain) on Nov 20, 2013 at 19:17 UTC

    If the script is being run in a cron job then the 'die' message should be sent in an email to the user the script is being run as, which all too often is "root" on the local machine. You might want to check that mailbox if you haven't done so already.

    That being said, I agree with adding some form of exception handling as mentioned in the prior response so you have a chance to send an informative email to the desired people.

    It helps to remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.
Re: How to send the "die" output to e-mail.
by taint (Chaplain) on Nov 20, 2013 at 20:19 UTC
    Greetingss.

    I'm guessing because of your use of the term "Command prompt". That you're on a Winboxen.

    What you might like to do, is simply do an APPEND to the file that would normally be created, as an attachment, when things work as intended. This would only need the addition of a simple eval to the sub you're already using. Maybe something like:

    or die "Sorry Dave. I'm afraid I can't do that"
    However, you'll need to use a redirect <, > for which ever text message you choose to use.

    OH, regarding cron, as mentioned earlier; do they still have the AT scheduler on Windows? That also might be a good alternative.

    HTH, and best wishes.

    --Chris

    #!/usr/bin/perl -Tw
    use Perl::Always or die;
    my $perl_version = (5.12.5);
    print $perl_version;
Re: How to send the "die" output to e-mail.
by pmu (Beadle) on Nov 21, 2013 at 06:54 UTC

    Respected Monks,

    Thank you for taking time to reply. I am running the script on a Windows 2003 (32 Bit) Server. My bad, I should have mentioned that earlier. Will try the options you have mentioned. Try::Tiny seems interesting and so do the other options. Thank you once again.

    || Aeterna Est Perspectum Cognitio ||

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1063573]
Approved by Laurent_R
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (16)
As of 2014-08-20 17:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (120 votes), past polls