如何通过 Windows CMD 捕获和显示任务的输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/278296/
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 to capture and display output from a task via Windows CMD
提问by JamShady
I've got a PHP script which I'm running from a command line (windows) that performs a variety of tasks, and the only output it gives is via 'print' statements which output direct to screen.
我有一个 PHP 脚本,我正在从执行各种任务的命令行(Windows)运行它,它提供的唯一输出是通过直接输出到屏幕的“打印”语句。
What I want to do is capture this to a log file as well.
我想做的是将其捕获到日志文件中。
I know I can do:
我知道我可以做到:
php-cli script.php > log.txt
But the problem with this approach is that all the output is written to the log file, but I can't see how things are running in the mean time (so I can stop the process if anything dodgy is happening).
但这种方法的问题是所有输出都写入日志文件,但我看不到同时运行情况(因此,如果发生任何不妥当,我可以停止该过程)。
Just to pre-empt other possible questions, I can't change all the print's to a log statement as there are far too many of them and I'd rather not change anything in the code lest I be blamed for something going fubar. Plus there's the lack of time aspect as well. I also have to run this on a windows machine.
只是为了抢占其他可能的问题,我无法将所有打印内容更改为日志语句,因为它们太多了,而且我宁愿不更改代码中的任何内容,以免因某些问题而受到指责。另外还有缺乏时间方面。我还必须在 Windows 机器上运行它。
Thanks in advance :)
提前致谢 :)
Edit: Thanks for the answers guys, in the end I went with the browser method because that was the easiest and quickest to set up, although I am convinced there is an actual answer to this problem somewhere.
编辑:感谢大家的回答,最后我选择了浏览器方法,因为这是最简单、最快速的设置,尽管我确信在某个地方有这个问题的实际答案。
回答by vfilby
You can create a powershell script that runs the command, reads the data from the command's STDOUT then outputs the output to both the log file and the terminal for you to watch. You can use the commands Write-Output and Write-Host.
您可以创建一个运行命令的 powershell 脚本,从命令的 STDOUT 读取数据,然后将输出输出到日志文件和终端供您观看。您可以使用命令 Write-Output 和 Write-Host。
Microsoft's site: http://www.microsoft.com/technet/scriptcenter/topics/msh/cmdlets/tee-object.mspx
微软网站:http: //www.microsoft.com/technet/scriptcenter/topics/msh/cmdlets/tee-object.mspx
Another option would be use find a tee program that will read input and divert it to two different outputs. I believe I have seen these for windows but I don't think they are standard.
另一种选择是使用 find tee 程序,该程序将读取输入并将其转移到两个不同的输出。我相信我已经在窗户上看到了这些,但我认为它们不是标准的。
Wikipedia: http://en.wikipedia.org/wiki/Tee_(command)
回答by Hyman Ryan
I have always opened the log file up in my web browser. This allows me to refresh it easily and does not interrupt any writing to the file that windows does. It isn't particularly elegant but it does work!
我总是在我的网络浏览器中打开日志文件。这使我可以轻松刷新它,并且不会中断 Windows 对文件的任何写入。它不是特别优雅,但确实有效!
回答by Jay
You want the "tee" command for Windows. See http://en.wikipedia.org/wiki/Tee_(command)
您需要 Windows 的“tee”命令。见http://en.wikipedia.org/wiki/Tee_(command)
Powershell includes a tee command, and there are also numerous versions of tee for Windows available, for instance:
Powershell 包含一个 tee 命令,并且还有许多适用于 Windows 的 tee 版本可用,例如:
Also can be implemented in VBScriptif you prefer.
如果您愿意,也可以在 VBScript 中实现。
EDIT: Just occurred to me I should also mention the tail command: http://en.wikipedia.org/wiki/Tail_(Unix). Tail allows you to read the last Nlines of a file, and also includes a "file monitor" mode that just continually displays the end of the file in real-time. This is perfect for log file monitoring since it allows you to watch the log in real-time without interfering with the process that's writing to the log. There are several implementations of tail for Windows, both command line and GUI based. Microsoft's Services For UNIX packages (or whatever they're calling it now) also include a version of tail. Some examples:
编辑:我刚刚想到我还应该提到 tail 命令:http: //en.wikipedia.org/wiki/Tail_(Unix)。Tail 允许您读取文件的最后N行,并且还包括“文件监视器”模式,该模式仅实时地持续显示文件的结尾。这非常适合日志文件监控,因为它允许您实时查看日志,而不会干扰写入日志的进程。有几种用于 Windows 的 tail 实现,包括基于命令行和 GUI 的。Microsoft 的 UNIX 服务包(或他们现在所称的任何名称)也包含一个版本的 tail。一些例子:
Some of these go far beyond just displaying the file in real-time as it updates and can send email alerts and colorize string matches, monitor multiple files at once, etc.
其中一些不仅仅是在文件更新时实时显示文件,还可以发送电子邮件警报和为字符串匹配着色,一次监控多个文件等。
回答by Charles Beattie
Slow:
减缓:
for /f "delims=" %a in ('php-cli script.php') do @echo %a&echo %a>>log.txt
or in a batch file:
或在批处理文件中:
for /f "delims=" %%a in ('php-cli script.php') do @echo %%a&echo %%a>>log.txt