Solved

Monitoring perl scripts for failure

Posted on 2011-03-08
10
387 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
parse a file and get data out 11 138
[Internet] Batch mode for shortening long URLs 3 34
Sleep one hour while in loop 4 43
Copy files and run psex to windows 7 only 14 51
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…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

734 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