Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Bash signal trap template. Bashでよく使うシグナルトラップのテンプレート。
#!/bin/bash
set -u # Check unset variables only
#set -ue # Check unset variables. Exit on error
LANG=C
# Trap signals
trap_HUP() {
echo "Trap HUP signal."
exit 1
}
trap_INT() {
echo "Trap INT signal."
exit 1
}
trap_TERM() {
echo "Trap TERM signal."
exit 1
}
trap_QUIT() {
echo "Trap QUIT signal."
exit 1
}
on_exit(){
echo "Kill child processes on exit by 'pkill -P $$'"
pkill -P $$
echo "Exit."
}
on_error() {
errcode=$?
echo "error line $1: command exited with status $errcode."
}
trap 'trap_HUP' HUP
trap 'trap_INT' INT
trap 'trap_QUIT' QUIT
trap 'trap_TERM' TERM
# bash pseudo-signals
trap 'on_exit' EXIT
trap 'on_error $LINENO' ERR
@sechiro
Copy link
Author

sechiro commented Feb 16, 2013

エラー時のトラップはこちらを参考にしました。
http://dharry.hatenablog.com/entry/20101121/1290273723

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment