bash 如何使用 tail 实用程序查看经常重新创建的日志文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6828130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I use tail utility to view a log file that is frequently recreated
提问by gfunk
I need a solution in creating a script to tail a log file that is recreated (with the same name) after it reaches a certain size.
我需要一个解决方案来创建一个脚本来跟踪一个在达到一定大小后重新创建(具有相同名称)的日志文件。
Using "tail -f" causes the tailing to stop when the file is recreated/rotated.
使用“ tail -f”会导致在重新创建/旋转文件时停止拖尾。
What I would like to do is create a script that would tail the file and after it reaches 100 lines for example, then restart the command... Or even better to restart the command when the file is recreated?
我想要做的是创建一个脚本来拖尾文件,例如,在它达到 100 行后,然后重新启动命令......或者甚至更好地在重新创建文件时重新启动命令?
Is it possible?
是否可以?
回答by evan
Yes! Use this (the retry will make tail retry when the file doesn't exist or is otherwise inaccessible rather than just failing - such as potentially when you are changing files):
是的!使用它(重试将在文件不存在或无法访问时进行尾部重试,而不仅仅是失败 - 例如可能在您更改文件时):
tail -f --retry <filename>
OR
或者
tail --follow=name --retry
OR
或者
tail -F <filename>
回答by xuuso
try running
尝试跑步
watch "tail -f" yourfile.log
回答by John W.
If tail -F is notavailable, and you are trying to recover from logrotate, you may add the copytruncateoption to your logrotate.d/spec file so instead of creating a new file each time after rotation, the file is kept and truncated, while a copy is rotated out.
如果 tail -F不可用,并且您正在尝试从logrotate恢复,您可以将该copytruncate选项添加到您的logrotate.d/规范文件中,以便每次旋转后都不会创建一个新文件,而是保留并截断文件,同时旋转副本出去。
This way the old file handle continues to point to the new (truncated)log file where new logs are appended.
这样,旧文件句柄继续指向(truncated)附加新日志的新日志文件。
Note that there may be some loss of data during this copy-truncateprocess.
请注意,在此copy-truncate过程中可能会丢失一些数据。
回答by Lynch
Since you dont have a tail that support all the features and because you dont have watch you could use a simple script that loop indefinitely to execute the tail.
由于您没有支持所有功能的尾部,并且因为您没有手表,所以您可以使用一个无限循环的简单脚本来执行尾部。
#!/bin/bash
PID=`mktemp`
while true;
do
[ -e "" ] && IO=`stat -c %i ""`
[ -e "" ] && echo "restarting tail" && { tail -f "" 2> /dev/null & echo $! > $PID; }
# as long as the file exists and the inode number did not change
while [[ -e "" ]] && [[ $IO = `stat -c %i ""` ]]
do
sleep 0.5
done
[ ! -z $PID ] && kill `cat $PID` 2> /dev/null && echo > $PID
sleep 0.5
done 2> /dev/null
rm -rf $PID
You might want to use trap to cleanly exit this script. This is up to you.
您可能希望使用 trap 干净地退出此脚本。这取决于你。
Basicaly, this script check if the inode number (using stat -c %i "$1") change to kill the tailcommand and start a new one when the file is recreated.
基本上,此脚本检查 inode 编号(使用stat -c %i "$1")是否更改以终止tail命令并在重新创建文件时启动一个新命令。
Note: you might get rid of the echo "restarting tail"which will pollute your output. It was only useful for testing. Also problems might occur if the file is replaced after we check the inode number and before we start the tail process.
注意:您可能会摆脱echo "restarting tail"会污染您的输出的 。它只对测试有用。如果在我们检查 inode 号之后和开始尾进程之前替换文件,也可能会出现问题。

