Solved

Monitoring perl scripts for failure

Posted on 2011-03-08
10
347 Views
Last Modified: 2013-12-25
Hi,
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/script.pl 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.


Thanks
0
Comment
Question by:Mohit_t
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 31

Accepted Solution

by:
farzanj earned 84 total points
ID: 35072171

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

Open in new window

0
 
LVL 31

Expert Comment

by:farzanj
ID: 35072380
Once this is done, you can capture the output of this file and email it
/path/to/script/script.pl argument1 argument2  2>&1 > file ; mutt -a file email@address.com

Open in new window

0
 
LVL 26

Assisted Solution

by:wilcoxon
wilcoxon earned 83 total points
ID: 35072679
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/script.sh argument1 argument2`
#!/bin/bash
# only capture errors
/path/to/script/script.pl $1 $2 2> file
# only send the email if there was an error
if [ -s file ] mailx -s 'script.pl error' email@address.com < file

Open in new window

0
 
LVL 11

Assisted Solution

by:tel2
tel2 earned 83 total points
ID: 35073199
Well said wilcocon, but when you wrote:
    if [ -s file ] mailx -s 'script.pl error' email@address.com < file
Have you tested that?  Doesn't work for me.

Didn't you mean this kind of thing:
    if [ -s file ]; then mailx -s 'script.pl error' email@address.com < file;fi
or:
    [[ -s file ]] && mailx -s 'script.pl error' email@address.com < file
?
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 35073443
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.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 31

Expert Comment

by:farzanj
ID: 35073585
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
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 35075910
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.
0
 

Author Comment

by:Mohit_t
ID: 35079310
Hi FishMonger,
I want to capture the runtime fatal error not the compilation errors.


Thanks
Mohit
0
 
LVL 11

Expert Comment

by:tel2
ID: 35079353
Hi Mohit_t,

What is wrong with wilcoxon's solution (and my amendment to it)?
Have you tried it?
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 35082358
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.

http://perldoc.perl.org/perlfaq8.html#How-can-I-do-an-atexit%28%29-or-setjmp%28%29/longjmp%28%29?-%28Exception-handling%29

http://www.google.com/search?hl=en&client=firefox-a&hs=hce&rls=org.mozilla%3Aen-US%3Aofficial&q=perl+exception+handling&aq=f&aqi=g3g-v7&aql=&oq=
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Making a simple AJAX shopping cart Couple years ago I made my first shopping cart, I used iframe and JavaScript, it was very good at that time, there were no sessions or AJAX, I used cookies on clients machine. Today we have more advanced techno…
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
The viewer will learn how to dynamically set the form action using jQuery.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now