子进程拥有用于重定向它的父进程的STD输出的文件 [英] child process owning the file used to redirect the std output of its parent process

查看:125
本文介绍了子进程拥有用于重定向它的父进程的STD输出的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

方案
我有一个批处理文件a.bat其中包含如下语句

Scenario I have a batch file a.bat which below contains statements

A.exe >> a.txt
Test.exe >> a.txt

A.exe时启动另一个exe文件说B.EXE(由_wpopen功能)并返回。但重定向A.exe时的输出后,A.TXT所有权似乎要转移到B.EXE。由于test.exe的不能重定向标准输出到A.TXT作为B.EXE仍在运行,并且仍然拥有A.TXT。

A.exe starts up another exe say b.exe (by _wpopen function) and returns. But after redirecting output of A.exe , ownership of a.txt seems to be transferred to b.exe. As test.exe cannot redirect its stdout to a.txt as b.exe is still running and still owns a.txt.

B.EXE无限期运行,不能等它停下来。
而且我也不能输出重定向到多个文件。由于这些EXE执行的安装设置的一部分,所以记录应该是连续的,必须做一个单独的文件

B.exe runs indefinitely, cannot wait for it to stop. And also I cannot redirect the output to multiple files. As these exe executions are a part of installation setup, so logging should be continuous and must be done to a single file

是否有任何可能的解决这种情况?我想要的文件a.text到A.EXE返回后是免费的!

Is there any possible solution to this scenario? I want the file a.text to be free after a.exe returns!

推荐答案

通过将重定向在每一行分别,你需要命令处理器重新打开文件。如果另一个进程仍然拥有原装手柄打开该文件,如您的方案,这是行不通的。

By putting the redirection on each line separately, you're requiring the command processor to reopen the file. If another process still has the original handle to the file open, as in your scenario, that won't work.

相反,重定向输出一次多个命令,或整个批处理文件。您可以使用调用做到这一点命令:

Instead, redirect the output once for multiple commands, or for the entire batch file. You can do this using the call command:

call :main >> a.txt
goto :eof

:main
A.exe
Test.exe
goto :eof

您可以选择使用括号:

(
A.exe
Test.exe
) > test.txt

或者,如果你只是想的部分的输出去的文件:

call :main 3>> a.txt
goto :eof

:main
A.exe >&3
Test.exe >&3
goto :eof

请直到B.EXE已经退出,日志文件将于打开,这样写的唯一方法是使用一个已经存在的句柄。 (假设你不能修改A.exe时。如果可以的话,那可能是一个更好的解决方案)

Keep in mind that until B.exe has exited, the log file will be held open, so the only way to write to it is to use an already existing handle. (This assumes that you cannot modify A.exe; if you can, that's probably a better solution.)

也有可能完全避免这个问题是这样的:

It might also be possible to avoid the problem altogether like this:

A.exe > temp.txt
type temp.txt >> a.txt

这篇关于子进程拥有用于重定向它的父进程的STD输出的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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