简体   繁体   English

Linux文件疯狂-奇怪的行为获取日志文件的最后一行

[英]Linux file craziness - strange behaviour getting last line of log file

I am trying to get the last line of a file. 我正在尝试获取文件的最后一行。 The file, ff.log, is getting new data every second while ffmpeg is working. ffmpeg工作时,文件ff.log每秒获取新数据。 The log file looks like this: 日志文件如下所示:

frame= 20 fps= 0 q=7.7 size= 40kB time=1.24 bitrate= 266.1kbits/s 帧= 20 fps = 0 q = 7.7大小= 40kB时间= 1.24比特率= 266.1kbits / s
frame= 30 fps= 28 q=6.6 size= 51kB time=1.90 bitrate= 218.4kbits/s 帧= 30 fps = 28 q = 6.6大小= 51kB时间= 1.90比特率= 218.4kbits / s
frame= 40 fps= 24 q=6.6 size= 61kB time=2.60 bitrate= 191.4kbits/s 帧= 40 fps = 24 q = 6.6大小= 61kB时间= 2.60比特率= 191.4kbits / s
frame= 47 fps= 20 q=6.8 size= 65kB time=3.08 bitrate= 173.8kbits/s 帧= 47 fps = 20 q = 6.8大小= 65kB时间= 3.08比特率= 173.8kbits / s
frame= 64 fps= 22 q=7.0 size= 84kB time=4.20 bitrate= 163.8kbits/s 帧= 64 fps = 22 q = 7.0大小= 84kB时间= 4.20比特率= 163.8kbits / s
(keeps adding new lines every second or faster) (保持每秒添加新行或更快的速度)

I have tried 我努力了

$line = `tail -n 1 $file`;

I tried using fseek() with a " php tail script " . 我尝试将fseek()与“ php tail script ”一起使用。

Both resulted in some strange behaviour. 两者都导致一些奇怪的行为。

I ran the my script from command line and it outputted: 我从命令行运行了脚本,并输出:

frame= XX fps= XX q=XX size= XX time=XX bitrate= XXkbits/s 帧= XX fps = XX q = XX大小= XX时间= XX比特率= XXkbits / s

Where XX kept increasing for several seconds until it was the value from the last line. XX持续增加几秒钟,直到它是最后一行的值。

Now, In my php script, I have 现在,在我的php脚本中,

echo "--$last_line--";

When I run it, the output is for several seconds, just the log line with increasing numbers. 当我运行它时,输出会持续几秒钟,只是日志行的数字不断增加。 When when it reaches the end, the output is 当到达终点时,输出为

--ame= 7119 fps= 9 q=13.8 size= 4809kB time=474.50 bitrate= 83.0kbits/s --ame = 7119 fps = 9 q = 13.8大小= 4809kB时间= 474.50比特率= 83.0kbits / s

Note that the first "--" collided with the $last_line and the other "--" is not there. 请注意,第一个“-”与$ last_line冲突,另一个“-”不存在。

What is the explanation for this strange behavior? 这种奇怪行为的解释是什么?


This is probably because it's thinking you're asking it to print "$lastline--" ... 这可能是因为它认为您正在要求它打印“ $ lastline-” ...

Try 尝试

echo '--' . $lastline . '--';

you might also want to do something like 您可能还想做类似的事情

var_dump($lastline); die();

To show you exactly what is in the variable, as this will give you more information. 为了确切显示变量中的内容,这将为您提供更多信息。

However, I suggest you post your whole script, so someone can try and see what the issue is. 但是,建议您发布整个脚本,以便有人可以尝试查看问题所在。

EDIT Looking at the file, it seems it's using ONLY \\r as a line terminator. 编辑查看文件,似乎它仅使用\\ r作为行终止符。 This causes the problems you see (it means "return to the start of the line") - this means that all of the log is showing as the last line. 这会引起您看到的问题(这意味着“返回到行的开头”)-这意味着所有日志都显示为最后一行。

Try using he command 尝试使用他命令

cat ff.log | tr "\r" "\n" | tail -n 2 | head -n 1

to get the last line of the log. 获取日志的最后一行。

Both the tail command and the PHP script have the same problem. tail命令和PHP脚本都有相同的问题。 They seek to the end of the file, back up to the previous end-of-line, then read and print everything from there to the end of the file. 他们寻找到文件的末尾,回到上一个行尾,然后读取并打印从那里到文件末尾的所有内容。

That's fine if the file is not growing very quickly. 如果文件增长不是很快,那很好。 When the file grows quickly you get multiple lines returned. 当文件快速增长时,您将返回多行。

Try either of these: 尝试以下任一方法:

$line = `tail -n 1 $file | tail -n 1`;
$line = `tail -n 1 $file | head`;

The first is slightly more accurate, but the second is faster. 第一个稍微准确一些,但是第二个更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM