Monitoring perl scripts for failure

I have a perl script which runs on a different host, I have to implement a proactive monitoring of this script, so that if at anytime the script fails to run I need to capture the error msg. in a text file and email it to the admin.
I am executing the script as
`rsh $host /path/to/script/ argument1 argument2`

I already have the email script which takes arguments and sends out the email.
Appreciate if someone can point me in the right direction on how to capture the errors when the script fails.

Who is Participating?

Improve company productivity with a Business Account.Sign Up

farzanjConnect With a Mentor Commented:

/path/to/script/ argument1 argument2  2>&1 > file

Open in new window

Once this is done, you can capture the output of this file and email it
/path/to/script/ argument1 argument2  2>&1 > file ; mutt -a file

Open in new window

wilcoxonConnect With a Mentor Commented:
farzanj's answer will work but will send you an email every time the script exits.

I would have a shell script that does something like the code attached and then call the shell script rather than the perl script via rsh.

`rsh $host /path/to/script/ argument1 argument2`
# only capture errors
/path/to/script/ $1 $2 2> file
# only send the email if there was an error
if [ -s file ] mailx -s ' error' < file

Open in new window

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

tel2Connect With a Mentor Commented:
Well said wilcocon, but when you wrote:
    if [ -s file ] mailx -s ' error' < file
Have you tested that?  Doesn't work for me.

Didn't you mean this kind of thing:
    if [ -s file ]; then mailx -s ' error' < file;fi
    [[ -s file ]] && mailx -s ' error' < file
Nope - I didn't test it (and should have noted that).  You're probably right about the syntax - I don't write shell scripts too often and often get syntax confused between the shells (as I've been places that require csh, others that require ksh, and some that don't care).

Your first suggestion is the closest to what I wanted.
I'm a little confused about your requirements.  If you can clarify, I can give u exactly what you need.  What email program are you using?  Other conditions
Please clarify if you need to trap if the scripts fails to execute (compilation failure) or if you need to trap when it encounters a fatal error during runtime.

If you need to capture/trap compilation failures, then wrapping it in a shell script might be the best approach.

If you need to catch  runtime issues, then I'd handle those directly in the Perl script, possibly via a custom $SIG{__DIE__} handler or use one of the exception handling modules on cpan.
Mohit_tAuthor Commented:
Hi FishMonger,
I want to capture the runtime fatal error not the compilation errors.

Hi Mohit_t,

What is wrong with wilcoxon's solution (and my amendment to it)?
Have you tried it?
The issue I see with the shell script wrapper is that it will not only send an email for fatal errors, but also for non fatal warnings since they also output on STDERR.  The OP specifically stated fatal errors.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.