# Set mode: e - exit on any error, x - write all commands to log #set -e # Address of admin __TMP__=${ADM_MAIL:=admin@sdsys.ru} # Create temp file for logging __TMP__=${LOG_FILE:=$(mktemp)} # Error mail subject __TMP__=${ERR_SUBJ="Error on "$1} exec 2>&1 exec &> >(tee -a "$LOG_FILE") function __cleanup() { if [[ $LOG_FILE == /tmp/tmp.* ]]; then rm -f $LOG_FILE fi } function __error_report() { err_h="======================== Trap Handler ==========================================" MYSELF="$0" # equals to my script name LASTLINE="$1" # argument 1: last line of error occurence LASTERR="$2" # argument 2: error code of last command echo "${MYSELF}: line ${LASTLINE}: exit status of last command: ${LASTERR}" # do additional processing: send email cat $LOG_FILE|mail -s "$ERR_SUBJ" \ -S smtp=${SMTP_SERVER} \ -S smtp-use-starttls \ -S smtp-auth=login \ -S ssl-verify=ignore \ -S smtp-auth-user=${JENKINS_MAIL_USER} \ -S smtp-auth-password=$(cat /run/secrets/jenkins-mail-pass) \ -S nss-config-dir=/etc/pki/nssdb \ -S from=${JENKINS_MAIL_USER} \ -a ${LOG_FILE} \ ${RECIPIENT_MAIL_BOX} } trap '__cleanup' SIGHUP SIGINT SIGTERM EXIT trap '__error_report ${LINENO} ${?}' ERR err_h="======================== Trap Handler =========================================="