我有一个脚本,可以在本地运行命令,也可以通过 SSH 在远程服务器上执行命令。当本地服务器上发生错误时,我能够捕获它,发送电子邮件并退出脚本。但当通过 SSH 的远程服务器发生错误时,此方法不起作用。关于如何实现这一目标有什么想法吗?
#!/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 [email protected]:/oracle/oem_dir
# import exp dump file into OEM2 schema on DEMO
ssh -t -t [email protected] <<'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
编辑:“解压缩 oeEXP.dmp.Z”时,远程服务器上发生错误。我需要将错误代码返回到本地服务器,以便我可以发送电子邮件并退出脚本。