7

我有以下脚本:

tail -f nohup.out
echo 5

当我按Ctrl+ Contail -f时,脚本停止运行:5未打印。echo 5停止命令后如何运行tail命令?

4

6 回答 6

10

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.

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

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. 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.

于 2012-08-14T13:07:48.203 回答
1
#!/bin/bash
trap "echo 5" SIGINT SIGTERM
tail -f nohup.out
于 2012-08-14T12:02:00.073 回答
0

您可以tail在新的 shell 中启动,如下所示:

#!/bin/bash
bash -i -c 'tail -f nohup.out'
echo 5
于 2012-08-14T13:31:43.060 回答
0

rwos 所述,但添加了内联陷阱。

bash -i -c "trap ' ' SIGINT; tail -F '$1'; trap - SIGINT"

在发出后续 SIGINT 信号时,这将在不关闭腻子会话的情况下工作(这是我的问题)

于 2016-04-12T07:52:11.903 回答
0

在 bash 中以单个 & 符号结束该行,以异步方式在后台运行命令

tail -f nohup.out &
于 2020-08-21T00:17:22.010 回答
-1

在 perl 中查找 File:Tail,在不同的项目中多次使用它来自动跟踪日志并执行日志输出的特定操作。

google ftp-upload-mon 我上传到 sourceforge 的一个项目,它使用 File:Tail to tail -f xferlogs

于 2012-08-14T15:37:16.610 回答