Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Bash signal trap template. Bashでよく使うシグナルトラップのテンプレート。
set -u # Check unset variables only
#set -ue # Check unset variables. Exit on error
# 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
echo "Kill child processes on exit by 'pkill -P $$'"
pkill -P $$
echo "Exit."
on_error() {
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment