bash 是否需要指定 EXIT 以外的陷阱?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8122779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it necessary to specify traps other than EXIT?
提问by William Pursell
I see a lot of shell scripts that do:
我看到很多 shell 脚本都这样做:
trap cmd 0 1 2 3 13 15 # EXIT HUP INT QUIT PIPE TERM
In every shell I have access to at the moment, all the traps other than 0 are redundant, and cmd will be executed upon receipt of a signal if the trap is simply specified:
在我目前可以访问的每个 shell 中,除 0 之外的所有陷阱都是多余的,如果只是指定了陷阱,那么 cmd 将在收到信号后执行:
trap cmd 0
Is the latter specification sufficient, or do some shells require the other signals to be specified?
后一个规范是否足够,或者某些 shell 是否需要指定其他信号?
采纳答案by Brandon Horsley
I think trap 0 is executed just prior to script termination in all cases, so is useful for cleanup functionality (like removing temporary files, etc). The other signals can have specialized error handling but should terminate the script (that is, call exit).
我认为陷阱 0 在所有情况下都在脚本终止之前执行,因此对于清理功能(如删除临时文件等)很有用。其他信号可以有专门的错误处理,但应该终止脚本(即调用退出)。
What you have described, I believe, would actually execute cmd twice. Once for the signal (for example SIGTERM) and once more on exit (trap 0).
我相信你所描述的实际上会执行 cmd 两次。一次用于信号(例如 SIGTERM),一次用于退出(陷阱 0)。
I believe the proper way to do this is like the following (see POSIX specification for trap):
我相信这样做的正确方法如下(请参阅 POSIX 规范trap):
trap "rm tmpfile" 0
trap "exit 1" TERM HUP ...
This ensures a temporary file is removed upon script completion, and lets you set custom exit statuses on signals.
这确保在脚本完成时删除临时文件,并允许您设置信号的自定义退出状态。
NOTE: trap 0 is called whether a signal is encountered or not.
注意:无论是否遇到信号,都会调用陷阱 0。
If you are not concerned with setting an exit status, trap 0 would be sufficient.
如果您不关心设置退出状态,陷阱 0 就足够了。
回答by carlo
To make sure the EXITsignal handler will not be executed twice (which is almost always not what you want) it should always set to be ignored or reset within the definition of the EXITsignal handler itself.
为了确保EXIT信号处理程序不会被执行两次(这几乎总是不是你想要的),它应该总是在EXIT信号处理程序本身的定义中设置为忽略或重置。
The same goes for signals that have more than one signal handler defined for them in a program.
对于在程序中为它们定义了多个信号处理程序的信号也是如此。
# reset
trap 'excode=$?; cmd; trap - EXIT; echo $excode' EXIT HUP INT QUIT PIPE TERM
# ignore
trap 'excode=$?; trap "" EXIT; cmd; echo $excode' EXIT HUP INT QUIT PIPE TERM
回答by William Pursell
The shell standard does not specify whether a trap on 0 is executed when an untrapped signal is received. In particular, bash and dash behave differently. Given trap cmd-list 0with no traps set for any signals, bash will execute the cmd-list upon receipt of SIGTERM, but dash will not. Given trap cmd-list 0 2, bash executes cmd-list once upon receipt of SIGTERM, and dash executes cmd-list twice.
shell 标准没有指定在接收到未捕获的信号时是否执行 0 上的陷阱。特别是,bash 和 dash 的行为不同。如果trap cmd-list 0没有为任何信号设置陷阱,bash 将在收到 SIGTERM 后执行 cmd-list,但 dash 不会。鉴于trap cmd-list 0 2,bash 在收到 SIGTERM 后执行 cmd-list 一次,dash 执行 cmd-list 两次。

