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.