我如何使用"启动"无子进程继承处理命令? [英] How do I use the "start" command without inheriting handles in the child process?

查看:157
本文介绍了我如何使用"启动"无子进程继承处理命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是说明我的问题一个最小的例子:

This is a minimal example that illustrates my problem:

:: test.bat
@echo off
call test2.bat 2>&1 | findstr foo
echo done calling test2

:: test2.bat
@echo off
start /B notepad >NUL
echo done starting child process

在此示例中,FINDSTR将无法完成,直到记事本被关闭,presumably因为记事本已经从标准输出父CMD进程继承。如何修改test2.bat使test.bat的不挂?

In this sample, findstr will not complete until notepad is closed, presumably because notepad has inherited stdout from the parent cmd process. How can I modify test2.bat so that test.bat doesn't hang?

推荐答案

我相信我能说明这个问题没有任何批处理文件在所有。下面的管道结构没有完成,直到NOTEPAD.EXE关闭后:

I believe I can illustrate the problem without any batch file at all. The following pipe construct does not complete until after notepad.exe is closed:

start notepad | findstr "^"

我期望记事本将在一个新的过程,完全由cmd.exe的管道被解除关联执行,而唯一被管道将START命令本身的输出(即无)。 START命令返回瞬间,但管道仍然开放,只要记事本运行。

I would expect that notepad would be executing in a new process that is totally disassociated from the cmd.exe pipe, and the only thing being piped would be the output of the START command itself (which is none). The START command returns "instantly", yet the pipe remains open for as long as notepad is running.

我不知道这是否是保持管道开启继承I / O流,但我可以证明,记事本进程的方式,可能是问题的根源继承流。这基本上是从小就在问题与输出重定向批量同样的问题。

I don't know if it is an inherited I/O stream that is keeping the pipe open, but I can prove that the notepad process is inheriting streams in a way that might be the root of the problem. This is basically the same issue that was brought up at Issue with output redirection in batch.

下面是一个简单的批处理脚本: test.bat的

Here is a simple batch script: test.bat

@echo off
call :test >nul
echo done calling test.bat
exit /b

:test
start notepad

请注意,标准输出已被重定向到NUL时,记事本将启动,由于重定向电话:试验> NUL

现在看一下,当我发出一系列命令的命令行,开始了与重定向到一个文件test.bat的有标准输出会发生什么:

Now look at what happens when I issue a series of commands from the command line, starting off with test.bat having stdout redirected to a file:

C:\test>test.bat >test.txt

C:\test>REM The command returns immediately, and notepad remains open

C:\test>type test.txt
done calling test.bat

C:\test>echo This fails as long as notepad remains open >test.txt
The process cannot access the file because it is being used by another process.

C:\test>type test.txt
done calling test.bat

C:\test>REM Now I close notepad

C:\test>echo This works once notepad is closed >test.txt

C:\test>type test.txt
This works once notepad is closed

C:\test>

我仍然这种行为交口称赞。这似乎完全不合逻辑的。

I am still blown away by this behavior. It seems totally illogical.

我不认为有什么办法prevent与CMD.EXE流继承。

I don't think there is any way to prevent the stream inheritance with cmd.exe.

也许哈利·约翰斯顿的建议可以解决(从问题的评论)问题:。我会写一个非常简单的可执行调用CreateProcess的禁用继承

Perhaps Harry Johnston's suggestion could solve the issue (from the question comments): "I'd write a very simple executable to call CreateProcess with inheritance disabled."

这篇关于我如何使用"启动"无子进程继承处理命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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