是否需要指定除 EXIT 之外的陷阱?

发布于 2024-12-15 09:56:22 字数 231 浏览 3 评论 0原文

我看到很多 shell 脚本都是这样做的:

trap cmd 0 1 2 3 13 15 # EXIT HUP INT QUIT PIPE TERM

在我目前可以访问的每个 shell 中,除 0 之外的所有陷阱都是多余的,如果简单地指定陷阱,则 cmd 将在收到信号后执行:

trap cmd 0

是后者规范是否足够,或者某些 shell 是否需要指定其他信号?

I see a lot of shell scripts that do:

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:

trap cmd 0

Is the latter specification sufficient, or do some shells require the other signals to be specified?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

盛装女皇 2024-12-22 09:56:22

我认为 trap 0 在所有情况下都会在脚本终止之前执行,因此对于清理功能(例如删除临时文件等)很有用。其他信号可以有专门的错误处理,但应该终止脚本(即调用 exit)。

我相信,您所描述的实际上会执行 cmd 两次。一次用于信号(例如 SIGTERM),一次用于退出(陷阱 0)。

我相信正确的方法如下(请参阅 POSIX 规范 trap):

trap "rm tmpfile" 0
trap "exit 1" TERM HUP ... 

这可确保脚本完成后删除临时文件,并允许您设置信号的自定义退出状态。

注意:无论是否遇到信号,都会调用 trap 0。

如果您不关心设置退出状态,陷阱 0 就足够了。

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).

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).

I believe the proper way to do this is like the following (see POSIX specification for 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.

If you are not concerned with setting an exit status, trap 0 would be sufficient.

淡紫姑娘! 2024-12-22 09:56:22

为了确保 EXIT 信号处理程序不会执行两次(这几乎总是不是您想要的),应始终在 EXIT 的定义中将其设置为忽略或重置> 信号处理程序本身。

对于在程序中为其定义了多个信号处理程序的信号也是如此。

# 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

To make sure the EXIT signal 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 EXIT signal handler itself.

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
茶花眉 2024-12-22 09:56:22

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 两次。

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 0 with 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文