-
-
Save bf4/b50e62678f2305bfdd707901cb91a3f9 to your computer and use it in GitHub Desktop.
Bash signal trap template.
Bashでよく使うシグナルトラップのテンプレート。
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 | |
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