tail -f命令后如何继续运行脚本 [英] how to continue run script after tail -f command

查看:654
本文介绍了tail -f命令后如何继续运行脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下脚本:

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 发送

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆