Created
November 21, 2012 07:35
-
-
Save pasela/4123609 to your computer and use it in GitHub Desktop.
[bash]error handling and error report example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# error handling and error report example | |
# | |
set -e | |
# logfile | |
LOGFILE=`mktemp` | |
on_error() | |
{ | |
# restore | |
exec 1>&3- 2>&4- | |
# send email, syslog and so on | |
echo "an error occured" | |
echo "----------------" | |
cat $LOGFILE | |
} | |
on_exit() | |
{ | |
# do something | |
rm -f $LOGFILE | |
} | |
trap "on_error" ERR | |
trap "on_exit" EXIT | |
# save STDOUT, STDERR and redirect to logfile | |
exec 3>&1 4>&2 | |
exec 1>$LOGFILE 2>&1 | |
# do something | |
echo hoge | |
do_something_in_which_errors_may_occur | |
echo done | |
# restore STDOUT, STDERR and close temporary descriptor | |
exec 1>&3- 2>&4- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
シェルスクリプト内で出力された内容を取っておいて、エラーが起きたらメールで送信したい、というようなことを言ってる人がいたので、ちょっと考えてみた。