简体   繁体   English

tail 和 less 命令不实时监控文件

[英]tail and less commands not monitoring file in real time

I'm looking for a way to monitor a file that is written to by a program on Linux.我正在寻找一种方法来监视由 Linux 上的程序写入的文件。 I found the tail -F command in here , and also recommended was less +FG .我在这里找到了tail -F命令,还推荐了less +FG I tested it by running tail -F file in one terminal, and a simple python script:我通过在一个终端中运行tail -F file和一个简单的 python 脚本来测试它:

import time

for i in range(20):
  print i
  time.sleep(0.5)

in another.在另一个。 I redirected the output to the file:我将输出重定向到文件:

python script.py >> file

I expected that tail would track the file contents and update the display in fixed intervals, instead it only shows what was written to the file after the command terminates.我预计tail会跟踪文件内容并以固定的时间间隔更新显示,而不是仅显示命令终止写入文件的内容。

The same thing happens with less +FG and also if I watch the output from cat .同样的事情发生在less +FG以及我观察cat的输出时。 I've also tried using the usual redirect which truncates the file > instead of >> .我也试过使用通常的重定向来截断文件>而不是>> Here it says the file was truncated, but still does not track it in real time.这里它说文件被截断了,但仍然没有实时跟踪它。

Any idea why this doesn't work?知道为什么这不起作用吗? (It's suggested here that it might be due to buffered writes, but since my script runs over 10 seconds, I suspect this might not be the cause) 这里建议它可能是由于缓冲写入,但由于我的脚本运行超过 10 秒,我怀疑这可能不是原因)

Edit: In case it matters, I'm running Linux Mint 18.1编辑:以防万一,我正在运行 Linux Mint 18.1

Python's standard out is buffered. Python 的标准输出是缓冲的。 If when you close the script / script is done, you see all the output - that's definitely buffer issue.如果关闭脚本/脚本完成后,您会看到所有输出 - 这绝对是缓冲区问题。

You can use this instead:您可以改用它:

import time
import sys

for i in range(20):
    sys.stdout.write('%d\n' % i)
    sys.stdout.flush()
    time.sleep(0.5)

I've tested it and it prints values in real time.我已经测试过它并实时打印值。 To overcome buffer issue, after each .write() method I use .flush() force "flushing" the buffer.为了克服缓冲区问题,在每个.write()方法之后,我使用.flush()强制“刷新”缓冲区。


Additional options from the comments:评论中的其他选项:

  • Use the original print statement with sys.stdout.flush() afterwords使用带有sys.stdout.flush()后缀的原始print语句
  • Run the python script with python -u for unbuffered binary stdout and stderr使用python -u为无缓冲的二进制标准输出和标准错误运行 python 脚本

Regarding jon1467 answer (sorry can't comment your answer), your understanding of redirection is wrong.关于 jon1467 答案(抱歉不能评论你的答案),你对重定向的理解是错误的。

Try this :试试这个:

dd if=/dev/urandom > test.txt

while looking at the file size with :在查看文件大小时:

ls -l test.txt

You'll see the file grow while dd is running.您会在 dd 运行时看到文件增长。

Vinny's answer is correct, python standard output is buffered. Vinny 的回答是正确的,python 标准输出被缓冲。 The more common way to the "buffering effect" you notice is by flushing the stdout as Vinny showed you.您注意到的“缓冲效果”的更常见方法是按照 Vinny 向您展示的刷新标准输出。

You could also use -u option to disable buffering for the whole python process, or you could just reopen standard output with a buffer size of 0 as below (in python2 at least):您还可以使用-u选项禁用整个 python 进程的缓冲,或者您可以重新打开标准输出,缓冲区大小为 0,如下所示(至少在 python2 中):

sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)

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

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