[英]how to continue run script after tail -f command
I have the following script:我有以下脚本:
tail -f nohup.out
echo 5
When I press Ctrl + C on tail -f
, the script stops running: the 5
is not printed.当我在tail -f
上按Ctrl + C时,脚本停止运行:不打印5
。 How can I run the command echo 5
after I stop the tail
command?停止tail
命令后如何运行命令echo 5
?
Ctrl + C sends the SIGINT signal to all the processes in the foreground process group . Ctrl + C向前台进程组中的所有进程发送SIGINT信号。 While tail
is running, the process group consists of the tail
process and the shell running the script.当tail
运行时,进程组由tail
进程和运行脚本的shell组成。
Use the trap
builtin to override the default behavior of the signal.使用内置trap
覆盖信号的默认行为。
trap " " INT
tail -f nohup.out
trap - INT
echo 5
The code of the trap does nothing, so that the shell progresses to the next command ( echo 5
) if it receives a SIGINT.陷阱的代码不执行任何操作,因此如果 shell 接收到 SIGINT,它将继续执行下一个命令 ( echo 5
)。 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).任何不做任何事情的 shell 代码都可以,除了一个空字符串,这意味着完全忽略信号(这不能使用,因为它会导致tail
也忽略信号)。 The second call to trap
restores the default behavior, so that after the third line a Ctrl + C will interrupt the script again.第二次调用trap
恢复默认行为,因此在第三行之后Ctrl + C将再次中断脚本。
#!/bin/bash
trap "echo 5" SIGINT SIGTERM
tail -f nohup.out
You could just start the tail
in a new shell, like so:你可以在一个新的 shell 中启动tail
,如下所示:
#!/bin/bash
bash -i -c 'tail -f nohup.out'
echo 5
在 bash 中,用一个与号结束该行,以异步方式在后台运行命令
tail -f nohup.out &
Look up File:Tail in perl, have used this a few times on different projects to automate tailing logs and carrying out a specific action for log output.在 perl 中查找 File:Tail,在不同的项目中多次使用它来自动跟踪日志并为日志输出执行特定操作。
google ftp-upload-mon a project I uploaded to sourceforge which uses File:Tail to tail -f xferlogs google ftp-upload-mon 一个我上传到 sourceforge 的项目,它使用 File:Tail to tail -f xferlogs
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.