有关于multiple bash traps for same signal的问题。相反的版本怎么样?可以写这样的东西:
sighand () {
case $1 in
1)
echo "CATCH: SIGHUP"
;;
2)
echo "CATCH: SIGINIT"
;;
...
# ALL EXCEPT 9
...
esac
};
trap sighand ALL
而不是:
sighand () {
echo "CATCH: TERM"
};
trap sighand TERM
答案 0 :(得分:4)
您必须为每个信号编写单独的函数:
manage.py makemigrations
作为一种解决方法,您可以编写自定义函数来为您设置所有陷阱。然后,您可以使用要处理的所有信号调用该函数:handle_hup () {
echo "CATCH: SIGHUP";
}
handle_int () {
echo "CATCH: INT";
}
trap handle_hup HUP
trap handle_int INT
_trap [sig1] [sig2] ...
答案 1 :(得分:0)
此答案应该做到:Identifying received signal name in Bash。我出于类似原因使用了它。对上述@ Leistungsabfall的较小扩展。此修订增加了通过名称指定要在陷阱中使用的功能的能力。此处的示例:
trap_with_arg() {
func="$1" ; shift
for sig ; do
trap "$func $sig" "$sig"
done
}
function handler() {
local SIGNAL=${1}
[ "${DEBUG}" -gt "0" ] && echo "Received Signal: ${SIGNAL}" >&2
case "${SIGNAL}" in
EXIT)
[ "${DEBUG}" -gt "0" ] && echo "EXIT clause"
# ^C
;;
USR2)
[ "${DEBUG}" -gt "0" ] && echo "DUSR2 clause"
;;
HUP|USR1)
[ "${DEBUG}" -gt "0" ] && echo "HUP|USR1 clause"
;;
ABRT)
[ "${DEBUG}" -gt "0" ] && echo "ABRT clause"
;;
INT|TERM)
[ "${DEBUG}" -gt "0" ] && echo "INT|TERM clause"
;;
esac
exit 0
}
trap_with_arg handler USR1 USR2 HUP INT ABRT TERM EXIT