windows 带有重定向输出的批处理后台进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7437666/
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
Background processes in batch with redirected output
提问by darckeen
I'm trying to run several background processes from a batch file and have the output directed to a file. Is it possible to do this in Windows? This is what I've tried but it end up directing the output of the start program rather then background process.
我试图从一个批处理文件运行几个后台进程,并将输出定向到一个文件。是否可以在 Windows 中执行此操作?这是我尝试过的,但它最终引导了启动程序的输出,而不是后台进程。
start myapp.exe > myapp.out 2>&1
采纳答案by a_horse_with_no_name
I think the only chance you have is to create one batch file for each exe that you want to start. Inside the batch file you can redirect the output. The master batch file would then "start" the batch file, not the exe directly.
我认为您唯一的机会是为您要启动的每个 exe 创建一个批处理文件。在批处理文件中,您可以重定向输出。然后,主批处理文件将“启动”批处理文件,而不是直接启动 exe。
You just need to include an exit
command at the end of each batch file:
您只需要exit
在每个批处理文件的末尾包含一个命令:
start_myapp.cmd
contains the following:
start_myapp.cmd
包含以下内容:
myapp.exe > myapp.out 2>&1
exit
then you can run
然后你可以运行
start start_myapp.cmd
and the output will be redirected
并且输出将被重定向
回答by dbenham
Actually it is quite easy without using a helper batch file. You just need to run the application via cmd.exe instead, and make sure to escape the special characters so they pass through to cmd.exe.
实际上,不使用帮助程序批处理文件很容易。您只需要通过 cmd.exe 运行应用程序,并确保对特殊字符进行转义,以便它们传递给 cmd.exe。
You probably don't want to see an extra console window, so use the START /B option.
您可能不想看到额外的控制台窗口,因此请使用 START /B 选项。
start /b "" cmd /c myapp.exe ^>myapp.out 2^>^&1
Each STARTed process must have its output directed to a unique file. Multiple processes cannot share the same output file.
每个 STARTed 进程必须将其输出定向到一个唯一的文件。多个进程不能共享同一个输出文件。