I have a script that run commands locally, and also execute commands on a remote server via SSH. When an error occurs on the local server, I am able to trap it, send an email and exit the script. But this is not working when an error occurs on the remote server over SSH. Any ideas on how to accomplish this?

# Data Pump export of OE schema to server

# trap and email errors
# add to the top of your script
f () {
   errcode=$? # save the exit code as the first thing done in the trap        function

v1="Error "
v3=" the command executing at the time of the error was "
v5=" on line "
v7="$v1 $v2 $v3 $v4 $v5 $v6"
SUBJECT="ERROR - oe export"

echo "$v7" | mailx -s "$SUBJECT" "$EMAIL_LIST"
exit $errcode  # or use some other value or do return instead
trap f ERR

set -e   # exit when a statement returns non-true value
set -u   # all variables must be set

# set environment variables
. $HOME/.oraenv

# start export
cd $HOME/oe_dir
exp userid=system/******@oem.qasrv parfile=par.txt

# compress export dump file
compress oeEXP.dmp

# send compressed dump file to DEMO server
scp oeEXP.dmp.Z oracle@

# import exp dump file into OEM2 schema on DEMO
ssh -t -t oracle@ <<'ENDSSH'
export ORACLE_HOME=/oracle/ora92
cd $HOME/oem_dir
uncompress oeEXP.dmp.Z   ########ERROR: out of disk space#########
imp system/******* file=oeEXP.dmp log=imp.log fromuser=oe touser=oem2 tables=\(*\) buffer=2000000

# remove exp dump file
/bin/rm -f $HOME/oe_dir/oeEXP.dmp.Z

EDIT: The error on the remote server occurs when "uncompress oeEXP.dmp.Z". I need to return the error code back to the local server so I can send an email and exit the script.

  • 3
    uncompress oeEXP.dmp.Z || exit 99 ## or whatever code you like between 1 and 255. Check $? after the ENDSSH.
    – Jeff Schaller
    Jan 29 '16 at 23:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.