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.

#!/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:

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.

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

Thanks giannicello.

I didn't know how to do error checking in the shell script. You gave me what I needed. Thank you.

Your suggestion worked. I received an email with the following message in the body.

Error parsing file (1)

How do I email the contents of the exception thread?

Thanks in advance.

think this'll work:


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:

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

Perfect. Thanks quirkasaurus. This worked great. I used your first example.

The exception thread should be stdout, redirect it (and stderr if you want) to the log file, then include the log in the body.

#!/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