2

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?

#!/bin/bash
#
# Data Pump export of OE schema to 10.10.10.10 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 "
v2=$errcode
v3=" the command executing at the time of the error was "
v4=$BASH_COMMAND
v5=" on line "
v6=${BASH_LINENO[0]}
v7="$v1 $v2 $v3 $v4 $v5 $v6"
SUBJECT="ERROR - oe export"
EMAIL_LIST="me@???.com"

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
export ORACLE_SID=OEM
. $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@10.10.10.10:/oracle/oem_dir

# import exp dump file into OEM2 schema on DEMO
ssh -t -t oracle@10.10.10.10 <<'ENDSSH'
export ORACLE_SID=OEM
export ORACLE_HOME=/oracle/ora92
export PATH=$ORACLE_HOME/bin:$PATH
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
exit
ENDSSH

# 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.

1
  • 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.