リモートサーバーのトラップエラー

リモートサーバーのトラップエラー

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」を解凍中にリモートサーバーでエラーが発生しました。電子メールを送信してスクリプトを終了するには、ローカルサーバーにエラーコードを返す必要があります。

ベストアンサー1

おすすめ記事