在 win32 上捕获尚未刷新的命令行输出
(上下文:我正在尝试从 Perl CGI 脚本监视长时间运行的进程。它备份 MSSQL 数据库,然后对其进行 7-zip 压缩。到目前为止,备份部分(使用 WITH STATS=1) 输出到一个文件,我可以让浏览器查看该文件,每隔几秒刷新一次,它就可以工作。)
我正在尝试使用 7zip 的命令行实用程序,但将进度条捕获到文件中。不幸的是,与 SQL 备份不同的是,每次完成另一个百分比时,它都会输出另一行,7zip 在输出新的进度数据之前会倒回其输出,因此如果您只是正常使用它,它看起来会更好。命令行。不幸的是,使用 >
、1>
和 2>
的正常重定向只会创建一个空白文件,并且不会输出出现在其中,但 >
除外,它在作业完成之前没有输出,这对于进度条来说不是很有用。
我怎样才能捕获这种输出,或者通过仅使用命令行技巧(没有 Perl)将 % 中的每个更改以某种方式附加到日志文件(这样我就可以使用现有的日志文件监视方法),或者使用一些 Perl调用system()
后直接捕获它的代码?
(Context: I'm trying to monitor a long-running process from a Perl CGI script. It backs up an MSSQL database and then 7-zips it. So far, the backup part (using WITH STATS=1
) outputs to a file, which I can have the browser look at, refreshing every few seconds, and it works.)
I'm trying to use 7zip's command-line utility but capture the progress bar to a file. Unfortunately, unlike SQL backups, where every time another percent is done it outputs another line, 7zip rewinds its output before outputting the new progress data, so that it looks nicer if you're just using it normally on the command-line. The reason this is unfortunate is that normal redirects using >
, 1>
, and 2>
only create a blank file, and no output ever appears in it, except for >
, which has no output until the job is done, which isn't very useful for a progress bar.
How can I capture this kind of output, either by having every change in % somehow be appended to a logfile (so I can use my existing method of logfile monitoring) just using command-line trickery (no Perl), or by using some Perl code to capture it directly after calling system()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您需要一次捕获所有输出,那么这就是您想要的代码:
如果您想逐行读取输出,那么您需要以下代码:
另一种选择是使用 Windows 创建过程。我知道 Windows C/C++ 创建过程将允许您重定向所有标准输出。 Perl 可以访问相同的 API 调用:请参阅 Win32::Process。
If you need to capture the output all at once then this is the code you want:
If you want to read the output line by line then you need this code:
Another option is using Windows create process. I know Windows C/C++ create process will allow you to redirect all stdout. Perl has access to this same API call: See Win32::Process.
您可以尝试打开 管道 来读取 7zip 的输出。
You can try opening a pipe to read 7zip's output.
这并没有回答如何捕获倒带的输出,但这是我最终使用的一种有用的方法。
对于恢复:
7za l
列出 zip 文件中的文件及其大小7za e
使用open my $command
-s $filename
输出,并备份:
7za a -w< /code>
.tmp
文件,.tmp
文件不再存在时,,您就完成了恢复,您将获得足够的数据来显示完成的百分比,但对于备份,您只能显示到目前为止的总文件大小,但如果您使用类似的数据来获得估计值,则可以与历史比率进行比较。尽管如此,反馈比以前更多(没有)。
This doesn't answer how to capture output that gets rewound, but it was a useful way of going about it that I ended up using.
For restores:
7za l
to list the files in the zip file and their sizes7za e
usingopen my $command
-s $filename
and compare to the listingFor backups:
7za a -w
.tmp
file in the dir.tmp
file no longer exists, you're doneFor restores you get enough data to show a percentage done, but for backups you can only show the total file size so far, but you could compare with historical ratios if you're using similar data to get a guestimate. Still, it's more feedback than before (none).