且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Bash脚本:无法正确处理SIGTSTP

更新时间:2023-02-15 22:50:14

在当前执行中的进程终止之前,shell不会执行陷阱. (至少,这是bash 3.00.15的行为).如果通过^ c发送SIGINT,则会将其发送到前台进程组中的所有进程;否则,它将被发送到前台进程组.如果当前正在执行的程序收到该消息并终止,则bash可以执行该陷阱.与SIGTSTP类似,通过^ z; bash接收到信号,但是直到正在运行的程序终止后才执行陷阱,如果采用默认行为并将其挂起,则bash不会执行该操作.尝试用简单的read f替换...,并注意陷阱会立即执行.

The shell does not execute the trap until the currently executing process terminates. (at least, that is the behavior of bash 3.00.15). If you send SIGINT via ^c, it is sent to all processes in the foreground process group; if the program currently executing receives it and terminates then bash can execute the trap. Similarly with SIGTSTP via ^z; bash receives the signal but does not execute the trap until the program that was being run terminates, which it does not do if it takes the default behavior and is suspended. Try replacing ... with a simple read f and note that the trap executes immediately.