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
Solved

Monitoring perl scripts for failure

Posted on 2011-03-08
10
376 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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Call Shell Script from Perl Script 6 133
combine multiple lines 2 67
copy-item script help 15 80
What is assert.deepEqual? 4 53
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 provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
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…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

808 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