Skip to content

Instantly share code, notes, and snippets.

@bf4
Forked from sechiro/bash_trap_template.sh
Created November 1, 2016 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bf4/b50e62678f2305bfdd707901cb91a3f9 to your computer and use it in GitHub Desktop.
Save bf4/b50e62678f2305bfdd707901cb91a3f9 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment