Get-Content -wait 无法按照文档中的描述工作 [英] Get-Content -wait not working as described in the documentation

查看:32
本文介绍了Get-Content -wait 无法按照文档中的描述工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到当 Get-Content path/to/logfile -Wait 时,输出实际上并不是每秒刷新一次,因为文档解释了它应该如此.如果我在 Windows 资源管理器中进入日志文件所在的文件夹并刷新文件夹,则 Get-Content 将输出日志文件的最新更改.

I've noticed that when Get-Content path/to/logfile -Wait, the output is actually not refreshed every second as the documentation explains it should. If I go in Windows Explorer to the folder where the log file is and Refresh the folder, then Get-Content would output the latest changes to the log file.

如果我在同一个日志文件上使用 cygwin 尝试 tail -f(与尝试 get-content 时不同),那么它会像一个人一样拖尾期待,无需我做任何事情即可实时刷新.

If I try tail -f with cygwin on the same log file (not at the same time than when trying get-content), then it tails as one would expect, refreshing real time without me having to do anything.

有人知道为什么会发生这种情况吗?

Does anyone have an idea why this happens?

推荐答案

Bernhard König 在评论中报告说,该问题终于在 Powershell 5 中得到修复.

你说的很对.Get-Content 上的 -Wait 选项会等待文件关闭,然后才能读取更多内容.可以在 Powershell 中演示这一点,但要正确使用循环可能会很棘手,例如:

You are quite right. The -Wait option on Get-Content waits until the file has been closed before it reads more content. It is possible to demonstrate this in Powershell, but can be tricky to get right as loops such as:

while (1){
get-date | add-content c:\tesetfiles\test1.txt 
Start-Sleep -Milliseconds 500
}

每次循环时都会打开和关闭输出文件.

will open and close the output file every time round the loop.

要演示该问题,请打开两个 Powershell 窗口(或 ISE 中的两个选项卡).在一个输入这个命令:

To demonstrate the issue open two Powershell windows (or two tabs in the ISE). In one enter this command:

PS C:\> 1..30 | % { "${_}: Write $(Get-Date -Format "hh:mm:ss")"; start-sleep 1 } >C:\temp\t.txt

这将运行 30 秒,每秒向文件中写入 1 行,但不会每次都关闭和打开文件.

That will run for 30 seconds writing 1 line into the file each second, but it doesn't close and open the file each time.

在另一个窗口中使用 Get-Content 读取文件:

In the other window use Get-Content to read the file:

get-content c:\temp\t.txt -tail 1 -wait | % { "$_ read at $(Get-Date -Format "hh:mm:ss")" }

使用 -Wait 选项,您需要使用 Ctrl+C 来停止命令,以便运行该命令 3 次,等待几秒钟在前两个中的每一个之后以及在第三个之后更长的等待给了我这个输出:

With the -Wait option you need to use Ctrl+C to stop the command so running that command 3 times waiting a few seconds after each of the first two and a longer wait after the third gave me this output:

PS C:\> get-content c:\temp\t.txt -tail 1 -wait | % { "$_ read at $(Get-Date -Format "hh:mm:ss")" }
8: Write 12:15:09 read at 12:15:09

PS C:\> get-content c:\temp\t.txt -tail 1 -wait | % { "$_ read at $(Get-Date -Format "hh:mm:ss")" }
13: Write 12:15:14 read at 12:15:15

PS C:\> get-content c:\temp\t.txt -tail 1 -wait | % { "$_ read at $(Get-Date -Format "hh:mm:ss")" }
19: Write 12:15:20 read at 12:15:20
20: Write 12:15:21 read at 12:15:32
21: Write 12:15:22 read at 12:15:32
22: Write 12:15:23 read at 12:15:32
23: Write 12:15:24 read at 12:15:32
24: Write 12:15:25 read at 12:15:32
25: Write 12:15:26 read at 12:15:32
26: Write 12:15:27 read at 12:15:32
27: Write 12:15:28 read at 12:15:32
28: Write 12:15:29 read at 12:15:32
29: Write 12:15:30 read at 12:15:32
30: Write 12:15:31 read at 12:15:32

由此我可以清楚地看到:

From this I can clearly see:

  1. 每次运行该命令时,它都会将最新的一行写入文件.即缓存没有问题,也没有需要刷新的缓冲区.
  2. 只读取一行,然后在另一个窗口中运行的命令完成之前不会再出现任何输出.
  3. 一旦它完成,所有待处理的行就会一起出现.这一定是由关闭文件的源程序触发的.

此外,当我在另外两个窗口中运行 Get-Content 命令重复练习时,一个窗口读取第 3 行然后等待,另一个窗口读取第 6 行,因此该行肯定正在写入到文件.

Also when I repeated the exercise with the Get-Content command running in two other windows one window read line 3 then just waited, the other window read line 6, so the line is definitely being written to the file.

-Wait 选项正在等待文件关闭事件,而不是等待广告中的 1 秒,这似乎很确定.文档有误.

It seems pretty conclusive that the -Wait option is waiting for a file close event, not waiting for the advertised 1 second. The documentation is wrong.

我应该补充一点,因为 Adi Inbar 似乎坚持认为我错了,我在这里给出的示例仅使用 Powershell,因为这似乎最适合 Powershell 讨论.我也确实使用 Python 验证了行为与我描述的完全一样:

I should add, as Adi Inbar seems to insistent that I'm wrong, that the examples I gave here use Powershell only as that seemed most appropriate for a Powershell discussion. I did also verify using Python that the behaviour is exactly as I described:

写入文件的内容可以通过新的 Get-Content -Wait 命令立即读取,前提是应用程序已刷新其缓冲区.

Content written to a file is readable by a new Get-Content -Wait command immediately provided the application has flushed its buffer.

使用 Get-Content -Wait 的 Powershell 实例不会在正在写入的文件中显示新内容,即使稍后启动的另一个 Powershell 实例看到了较晚的数据.这最终证明 Powershell 可以访问数据,并且 Get-Content -Wait 不是以 1 秒的间隔轮询,而是在下次查找数据之前等待某个触发事件.

A Powershell instance using Get-Content -Wait will not display new content in the file that is being written even though another Powershell instance, started later, sees the later data. This proves conclusively that the data is accessible to Powershell and Get-Content -Wait is not polling at 1 second intervals but waiting for some trigger event before it next looks for data.

dir 报告的文件大小在添加行时正在更新,因此不是 Powershell 等待目录条目大小更新的情况.

The size of the file as reported by dir is updating while lines are being added, so it is not a case of Powershell waiting for the directory entry size to be updated.

当写入文件的进程关闭它时,Get-Content -Wait 几乎立即显示新内容.如果它一直等到数据被刷新到磁盘,那么在 Windows 刷新它的磁盘缓存之前会有一个延迟.

When the process writing the file closes it, the Get-Content -Wait displays the new content almost instantly. If it were waiting until the data was flushed to disk there would be up to a delay until Windows flushed it's disk cache.

@AdiInbar,恐怕你不明白Excel 在保存文件时的作用.仔细看看.如果您正在编辑 test.xlsx,那么在同一文件夹中还有一个隐藏文件 ~test.xlsx.使用 dir ~test.xlsx -hidden |选择 CreationTime 以查看它的创建时间.保存您的文件,现在 test.xlsx 将从 ~test.xlsx 获得创建时间.换句话说,在 Excel 中保存到 ~ 文件,然后删除原始文件,将 ~ 文件重命名为原始名称并创建一个新的 ~文件.那里有很多开始和结束.

@AdiInbar, I'm afraid you don't understand what Excel does when you save a file. Have a closer look. If you are editing test.xlsx then there is also a hidden file ~test.xlsx in the same folder. Use dir ~test.xlsx -hidden | select CreationTime to see when it was created. Save your file and now test.xlsx will have the creation time from ~test.xlsx. In other words saving in Excel saves to the ~ file then deletes the original, renames the ~ file to the original name and creates a new ~ file. There's a lot of opening and closing going on there.

在您保存之前,您正在查看的文件已打开,在该文件打开后,但它是一个不同的文件.我认为 Excel 是一个过于复杂的场景,无法确切说明是什么触发了 Get-Content 以显示新内容,但我确定您误解了它.

Before you save it has the file you are looking at open, and after that file is open, but its a different file. I think Excel is too complex a scenario to say exactly what triggers Get-Content to show new content but I'm sure you mis-interpreted it.

这篇关于Get-Content -wait 无法按照文档中的描述工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆