err_trap 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # Set mode: e - exit on any error, x - write all commands to log
  2. #set -e
  3. # Address of admin
  4. __TMP__=${ADM_MAIL:=admin@sdsys.ru}
  5. # Create temp file for logging
  6. __TMP__=${LOG_FILE:=$(mktemp)}
  7. # Error mail subject
  8. __TMP__=${ERR_SUBJ="Error on "$1}
  9. exec 2>&1
  10. exec &> >(tee -a "$LOG_FILE")
  11. function __cleanup()
  12. {
  13. if [[ $LOG_FILE == /tmp/tmp.* ]]; then
  14. rm -f $LOG_FILE
  15. fi
  16. }
  17. function __error_report()
  18. {
  19. err_h="======================== Trap Handler =========================================="
  20. MYSELF="$0" # equals to my script name
  21. LASTLINE="$1" # argument 1: last line of error occurence
  22. LASTERR="$2" # argument 2: error code of last command
  23. echo "${MYSELF}: line ${LASTLINE}: exit status of last command: ${LASTERR}"
  24. # do additional processing: send email
  25. cat $LOG_FILE|mail -s "$ERR_SUBJ" \
  26. -S smtp=${SMTP_SERVER} \
  27. -S smtp-use-starttls \
  28. -S smtp-auth=login \
  29. -S ssl-verify=ignore \
  30. -S smtp-auth-user=${JENKINS_MAIL_USER} \
  31. -S smtp-auth-password=$(cat /run/secrets/jenkins-mail-pass) \
  32. -S nss-config-dir=/etc/pki/nssdb \
  33. -S from=${JENKINS_MAIL_USER} \
  34. -a ${LOG_FILE} \
  35. ${RECIPIENT_MAIL_BOX}
  36. }
  37. trap '__cleanup' SIGHUP SIGINT SIGTERM EXIT
  38. trap '__error_report ${LINENO} ${?}' ERR
  39. err_h="======================== Trap Handler =========================================="