Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Have script send email if it dies

by Asim (Hermit)
on Jul 21, 2006 at 12:57 UTC ( [id://562849]=note: print w/replies, xml ) Need Help??


in reply to Have script send email if it dies

More complex, yet also more robust (in my opinion) is to use Log::Log4perl along with Log::Dispatch::Email::MailSender, which does the trick well in my production code. Here's an example of how it captures the die for logging; you'd configure for emailing elsewhere, and the docs explain how:

$SIG{__DIE__} = sub { $Log::Log4perl::caller_depth++; my $logger = get_logger("YOUR_LOGGER_HERE"); $logger->fatal(@_); die @_; # Now terminate really };

This code is originally from the Log::Log4perl FAQ. The nice thing about using a logger, is that you can have it track "state"; sometimes simply having the dying error message isn't enough to track down an issue.

slight edit for clarification of email portion.

----Asim, known to some as Woodrow.

Replies are listed 'Best First'.
Re^2: Have script send email if it dies
by Anonymous Monk on Jul 21, 2006 at 15:34 UTC
    As sgifford pointed out, this won't catch errors in perl; running out of memory, etc. whereas an external monitoring program will. That's probably the most robust solution.

    Your logging code can then generate whatever output you want, and you'll only get an email describing the code state when the program terminates abnormally (dies, exits with an error status, etc.)

      ...this won't catch errors in perl; running out of memory, etc. whereas an external monitoring program will. That's probably the most robust solution.

      You're correct; my focus was on solutions "within the script", actually. Thanks for the note!

      ----Asim, known to some as Woodrow.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://562849]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (1)
As of 2025-01-13 10:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which URL do you most often use to access this site?












    Results (28 votes). Check out past polls.