tail -f命令后如何继续运行脚本 [英] how to continue run script after tail -f command
问题描述
我有以下脚本:
tail -f nohup.out
echo 5
当我按tail -f
上的 Ctrl + C 时,脚本停止运行:5
未打印.停止tail
命令后如何运行命令echo 5
?
When I press Ctrl+C on tail -f
, the script stops running: the 5
is not printed. How can I run the command echo 5
after I stop the tail
command?
推荐答案
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.
这篇关于tail -f命令后如何继续运行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!