Send email on script error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Send email on script error
# 1  
Old 02-04-2009
Send email on script error

I need to start off by saying that I am not much of a programmer and know enough to cause lots of trouble.

I've been writing this script to decrypt an XML feed, then parse the feed into a database. The script is executed from cron.

Code:
#!/bin/sh

PGPPATH=/path/to/directory
        echo "pgp directory $PGPPATH"
        if [ -f ${PGPPATH}/*.pgp ];
        then
        for FILE in ${PGPPATH}/*.pgp;
                do
                        echo "decrypting $FILE"
                        /bin/echo mypassword | /usr/bin/gpg --no-tty --batch --passphrase-fd 0 --quiet --output ${FILE%.pgp}.xml --decrypt $FILE
                        echo "parsing xml"
                        /usr/bin/java -jar /root/XmlParse/XmlParser.jar "${FILE%.pgp}.xml" "jdbc:mysql://127.0.0.1/mydb" "admin" "mypassword"
                        echo "moving $FILE to ${PGPPATH}/feed_archive"
                        mv $FILE ${PGPPATH}/feed_archive
                        echo "moved $FILE to ${PGPPATH}/feed_archive";
                        mv ${PGPPATH}/*.xml ${PGPPATH}/xml
                        echo "mv ${PGPPATH}/*.xml ${PGPPATH}/xml";
                done
        else
        echo "pgp files do not exists"
        fi

The results are stored in a log. Here is an example of the log entry:

Code:
pgp directory /path/to/directory
decrypting /path/to/directory/IL_200902040703.pgp
parsing xml
working...
completed!
moving /path/to/directory/IL_200902040703.pgp to /path/to/directory/feed_archive
moved /path/to/directory/IL_200902040703.pgp to /path/to/directory/feed_archive
mv /path/to/directory/*.xml /path/to/directory/xml

Occasionally, the XmlParser.jar fails. I know it fails because the log above is missing the "completed!". When I run the script form the command line, I can see a java error stack. What I need is to have an email triggered containing the java error when the XmlParser.jar fails. Thanks in advance for any suggestions.
# 2  
Old 02-04-2009
Why can't you do error checking after that java call?

/usr/bin/java -jar /root/XmlParse/XmlParser.jar "${FILE%.pgp}.xml" "jdbc:mysql://127.0.0.1/mydb" "admin" "mypassword"

rc=$?
if [[ $rc -ne 0 ]]; then
echo "Error parsing file ($rc)"|mailx -s "Subject" toaddress
else
echo "Moving $FILE to ${PGPPATH}/feed_archive..."
etc...
fi
# 3  
Old 02-05-2009
Thanks giannicello.

I didn't know how to do error checking in the shell script. You gave me what I needed. Thank you.
# 4  
Old 02-05-2009
Your suggestion worked. I received an email with the following message in the body.

Code:
Error parsing file (1)

How do I email the contents of the exception thread?

Thanks in advance.
# 5  
Old 02-05-2009
think this'll work:

Code:
java stuff . .. . . . > java.log 2>&1

rc=$?

if [[ $rc -ne 0 ]]; then

(
echo "Error parsing file ($rc)"
echo
cat java.log
) |mailx -s "Subject" toaddress

fi

oh you mean the log of the whole shell script????

try putting everything into a function:

Code:
all_of_my_code()
{

blah
blah
blah

if [ any-errors ] then
  return 1
fi
}


all_of_my_code > log 2>&1

if [ $? -ne 0 ]; then
  (
  echo problems
  echo
  cat log
  ) | mailx to_me
fi

# 6  
Old 02-05-2009
Perfect. Thanks quirkasaurus. This worked great. I used your first example.
# 7  
Old 02-05-2009
The exception thread should be stdout, redirect it (and stderr if you want) to the log file, then include the log in the body.

Code:
#!/bin/sh
 
exec 1>$0".log"            #just stdout
exec > $0".log" 2>&1    #stdout and stderr
.
.
.

no need to put the code in a function, just add this before your code (only one of the lines). then cat the log into the mail body as directed earlier
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need little script to send out email

Hi Scripters, good day. bash-4.2# df -g /apps/prd Filesystem GB blocks Free %Used Iused %Iused Mounted on /dev/xxx 64.00 4.35 94% 1269284 8% /xxx bash-4.2# I was wondering if there is a script when the usage of the mountpoint above hit 98%, email would be... (3 Replies)
Discussion started by: jaapar
3 Replies

2. Shell Programming and Scripting

Script to send email if count is >1

i have below code to count number of rows in file1.txt, if the row count is more than one then i have sending an email along with file1.txt attached and fail the process(do nothing if count is <=1), if I test individually count part works good but when i include the email part its not working,... (4 Replies)
Discussion started by: srini_106
4 Replies

3. Shell Programming and Scripting

how to send html email from shell script

This is my below script which is working fine as it send only plain text email. So My question is- How can I modify my below script to send this as an HTML formatted email? I want to show the text color of this expression `(echo "100*$TEST2/$TEST1" | bc -l)` as `RED` in an email and that can be... (3 Replies)
Discussion started by: raihan26
3 Replies

4. Shell Programming and Scripting

Help with shell script to send email once

Hi Guys, I have this script which will monitor oracle db process if up or down.And I want it to send email if it's down and the time it's back to online. However my script just keep on sending "Email Up" if the db is up or "Email Down" if the db is down.Is there any way to trap it so that it... (5 Replies)
Discussion started by: d3xt3r
5 Replies

5. Shell Programming and Scripting

Script to send email after comparing the folder permissions to a certain permission & send email

Hello , I am trying to write a unix shell script to compare folder permission to say drwxr-x-wx and then send an email to my id in case the folders don't have the drwxr-x-wx permissions set for them . I have been trying to come up with a script for few days now , pls help me:( (2 Replies)
Discussion started by: nairshar
2 Replies

6. Shell Programming and Scripting

How to send email through shell script

Hi All, I am new to the unix , i have to deliver one script very urgently I have to write a shell script where i have i want to send email to specific email id in this script i want FROM to be parameterized and stored in a variable TO to be parameterized and stored in a variable... (3 Replies)
Discussion started by: nileshbhawsar
3 Replies

7. Shell Programming and Scripting

send an email of script output

Hi All, I'm trying to send some file which generated by script to my email. when I run the script I'm getting an email. Thats fine. But it seems to be all messed up like below Memory Status on ServerA: Mem: 3867444k total, 862680k used, 3004764k free, 54456k buffers!! CPU Status on ServerA:... (4 Replies)
Discussion started by: s_linux
4 Replies

8. Shell Programming and Scripting

script to send a file in email

a file is created on a daily basis in the name xyz_pqr_20071207.dat.i want to send the file as an attachment if the file contains more than 50 records.how can i write a script such that it will transmit the file after the file is created.i want to sed the file to say asdf@xyz.com. please help me... (2 Replies)
Discussion started by: dr46014
2 Replies

9. Shell Programming and Scripting

error-when trying to send the message thru email.

Hi All, I want to send a message through email. I have written below code. But it is not worling. Anybody has idea, why it is not working?. export $file1=$home1/pip1.$$ mailx -s "This Message from unix" abc@yahoo.com< $file1 thanks,Mary. (5 Replies)
Discussion started by: MARY76
5 Replies

10. Shell Programming and Scripting

script that will send and email attachment

I'm looking for a sample of some code that will take the output from a file and generate an email that will include that text as an attachment. the script is in the borne shell. any help? (2 Replies)
Discussion started by: davels
2 Replies
Login or Register to Ask a Question