大网数据广告活动
服务器运维问答

tail -F still stops on logrotate sometimes

· 52CE 我爱测

Linux ip-10-10-64-122 4.1.7-15.23.amzn1.x86_64 #1 SMP Mon Sep 14 23:20:33 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux I'm using tail -F in a bash script which in turn loops over the results. This script is supposed to run indefinitel…

问题原文

原帖链接 · pstanton · CC BY-SA 4.0

Linux ip-10-10-64-122 4.1.7-15.23.amzn1.x86_64 #1 SMP Mon Sep 14
23:20:33 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

I'm using tail -F in a bash script which in turn loops over the results. This script is supposed to run indefinitely

tail -F /tmp/my.log |
grep --line-buffered "xyz" |
while read i
do
    echo "$i"
done
echo "end of read"

however while sometimes it works through a logrotate, other times it does not:

tail: '/tmp/my.log' has become inaccessible: No such file or directory

In this case I do not see "end of read" meaning the tail/while is still running/stuck. I can confirm the log file does get put back in place after logrotate within milliseconds as you would expect.

Logrotate config:

/tmp/mosquitto.log {
            rotate 500
            compress
            maxsize 40M
            nocreate
            missingok
            postrotate
                    /usr/bin/killall -HUP mosquitto
            endscript
    }

How can I force this to continue to tail?

已采纳解答

原帖链接 · ku4eto · CC BY-SA 4.0

As i suspected, your log rotation basically renames the current log file, and then creates a new one. You need to use the copytruncate logrotate option, which keeps the original log file, along with all of its properties. This will allow tail to follow it properly. tail cannot follow a file, that does not exist anymore (the case when copytruncate is not used).

copytruncate: Truncate the original log file in place after creating a copy, instead of moving the old log file and optionally creating a new one. It can be used when some program cannot be told to close its logfile and thus might continue writing (appending) to the previous log file forever. Note that there is a very small time slice between copying the file and truncating it, so some logging data might be lost. When this option is used, the create option will have no effect, as the old log file stays in place.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2021-02-04)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。