简体   繁体   English

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

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

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.但事实证明,重定向是由 start 获取的,因此我得到了一个带有start结果的 0 字节文件 - 即什么都没有。 Is there any way to make the output redirection attach in some way to the output of my_program?有没有办法让输出重定向以某种方式附加到 my_program 的输出?

I've experimented with escaping, and neither ^2> nor 2^> seem to work.我已经尝试过转义,但^2>2^>似乎都不起作用。 Any help would be greatly appreciated!任何帮助将不胜感激!

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"

Use /B switch.使用 /B 开关。 No new window created and redirections remain, but the command is run in background, just as needed.没有创建新窗口并保留重定向,但该命令会根据需要在后台运行。

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

test.bat:测试.bat:

@echo off
echo bbb
sleep 10
echo ccc
exit

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阿布舍克

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

The following syntax seems to do the trick:以下语法似乎可以解决问题:

start /wait /b my_program.exe

This runs the program in the background and waits for it to complete.这会在后台运行程序并等待它完成。 Redirect stdout and stderr as you prefer:根据需要重定向 stdout 和 stderr:

start /wait /b my_program.exe > output.txt 2>&1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM