有什么方法可以重定向 Windows 命令行中使用“start”运行的命令的 stderr 输出?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1309441/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:00:59  来源:igfitidea点击:

Is there any way to redirect stderr output from a command run with "start" in the Windows command line?

windowsredirectcommandstderr

提问by basin

I have a program that I want to automate runs for, since it takes awhile to complete. For some reason it outputs everything to stderr instead of stdout, and I'd like to check on its progress, so I find myself needing to redirect stderr output within a start command.

我有一个程序要自动运行,因为它需要一段时间才能完成。出于某种原因,它将所有内容输出到 stderr 而不是 stdout,我想检查它的进度,所以我发现自己需要在 start 命令中重定向 stderr 输出。

I tried this:

我试过这个:

start "My_Program" "C:\Users\Me\my_program.exe" --some --presets --for 
--my_program.exe --output "C:\Users\Me\output_file_for_my_program" 
"C:\Users\Me\input_file_for_my_program" 2>"C:\Users\Me\my_program_output.log"

But it turns out that the redirect is being picked up by start, so that I get a 0-byte file with the result of start- namely, nothing. Is there any way to make the output redirection attach in some way to the output of my_program?

但事实证明,重定向是由 start 获取的,因此我得到了一个 0 字节的文件,结果为start- 即什么都没有。有没有办法让输出重定向以某种方式附加到 my_program 的输出?

I've experimented with escaping, and neither ^2>nor 2^>seem to work. Any help would be greatly appreciated!

我已经尝试过逃跑,既不^2>2^>似乎工作。任何帮助将不胜感激!

回答by Evan Anderson

Try this:

尝试这个:

start "My_Program" "%SystemRoot%\System32\cmd.exe" /c ""C:\Users\Me\my_program.exe" --some --presets --for --my_program.exe --output "C:\Users\Me\output_file_for_my_program" "C:\Users\Me\input_file_for_my_program" 2>"C:\Users\Me\my_program_output.log""

Obviously, not having "My Program" here I can't test this, per se. If we take that the built-in "FIND.EXE" command returns "File not found - filename" on STDERR, the following is working for me:

显然,这里没有“我的程序”,我本身无法对此进行测试。如果我们认为内置的“FIND.EXE”命令在 STDERR 上返回“找不到文件 - 文件名”,则以下内容对我有用:

start "My_Program" "%SystemRoot%\System32\cmd.exe" /c "find /v /i "blarg" "c:\not a real file.txt" 2> C:\stderr.txt"

回答by basin

Use /B switch. No new window created and redirections remain, but the command is run in background, just as needed.

使用 /B 开关。没有创建新窗口并保留重定向,但该命令会根据需要在后台运行。

start /B test.bat >test.txt <nul

test.bat:

测试.bat:

@echo off
echo bbb
sleep 10
echo ccc
exit

回答by MattB

How about putting the call to your command with the redirects in a batch file, and using start on the batch file?

如何通过批处理文件中的重定向调用您的命令,并在批处理文件上使用 start?

回答by Abhishek Nagar

I used the following command and it worked:

我使用了以下命令并且它起作用了:

start /affinity 2 /wait cmd.exe /C myprog.exe parameter1 parameter2 1^> .\a.log 2>> .\b.log

start /affinity 2 /wait cmd.exe /C myprog.exe parameter1 parameter2 1^> .\a.log 2>> .\b.log

Ref: http://www.pcreview.co.uk/forums/redirect-standard-output-w-start-command-t1467634.html

参考:http: //www.pcreview.co.uk/forums/redirect-standard-output-w-start-command-t1467634.html

Abhishek

阿布舍克