且构网

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

tail -f命令后如何继续运行脚本

更新时间:2023-12-05 19:20:46

Ctrl + C 发送进程组.在运行tail时,进程组由tail进程和运行脚本的shell组成.

Ctrl+C sends the SIGINT signal to all the processes in the foreground process group. While tail is running, the process group consists of the tail process and the shell running the script.

使用内置的 trap 覆盖默认行为信号.

Use the trap builtin to override the default behavior of the signal.

trap " " INT
tail -f nohup.out
trap - INT
echo 5

陷阱的代码不执行任何操作,因此如果外壳程序收到SIGINT,则外壳将继续执行下一个命令(echo 5).请注意,第一行中的引号之间有一个空格;任何不执行任何操作的shell代码都将起作用,除了一个空字符串,这将意味着完全忽略该信号(无法使用此字符串,因为它也会导致tail也忽略该信号).对trap的第二次调用将恢复默认行为,因此在第三行之后, Ctrl + C 将再次中断脚本.

The code of the trap does nothing, so that the shell progresses to the next command (echo 5) if it receives a SIGINT. Note that there is a space between the quotes in the first line; any shell code that does nothing will do, except an empty string which would mean to ignore the signal altogether (this cannot be used because it would cause tail to ignore the signal as well). The second call to trap restores the default behavior, so that after the third line a Ctrl+C will interrupt the script again.