Solved

Monitoring perl scripts for failure

Posted on 2011-03-08
10
367 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 12

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
 
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 12

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

810 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