Redirect output from a command within a batch file whose output is already redirected?

邮差的信 提交于 2019-12-30 07:13:34

问题


I've searched high and low, but all the suggestions and tips I've found do not work for some reason. I have a batch file that is being invoked as such:

cmd /C "automateMyProgram.bat >> automation.log 2>>&1"

That works great: automation.log gets loaded with all the stdout and stderr for that particular batch file. However, within that batch script I have the following command:

start php updateDB.php param1 param2 ^> updateDB.log

The php script does get executed just fine and reads in the parameters just fine, but updateDB.log is never created. I ensured that php error reporting in the php.ini file is set to output errors to the command line interface. There are several echo statements within the php script that I need to have recorded to a log, but they are not being output for some reason. I read that if you use the start command to invoke a program, you must use the caret operator to redirect output since the program is started in a new process. I also tried:

start php updateDB.php param1 param2 >> updateDB.log

and that didn't work either. So I then tried:

start /B "Database Update" "php" "param1" "param2" >> updateDB.log

and that didn't work from within the batch file, but it did when I copy and pasted it directly into a cmd window on the desktop.

Might any of you know how I can redirect the output of the php script being called from a batch file?

Thanks for your time and help!


回答1:


This is probably the simplest solution:

start cmd /c "php updateDB.php param1 param2 > updateDB.log"

or, if you want to include error messages in updateDB.log,

start cmd /c "php updateDB.php param1 param2 > updateDB.log 2>&1"



回答2:


Here is a sample of code that worked in a BATCH file launching VBScripts with the START command. It will lauch the seperate process and put the results into the specified log file.

rem **Setting the passed device to run against to a varible**
SET Audit_Device=%1

rem     **Launching "_SomeScript.vbs" in a seperate window**
start "WindowTitle" /d%~dp0  cmd /C cscript.exe ^"%~dp0_SomeScript.vbs^" %Audit_Device% ^>^"%~dp0%Audit_Device%_SomeScriptLogFile.txt^"

rem       **Launching "ListPermissionsForAllShares-Basic-and-Detailed.vbs" in a seperate window**
start "List Share Permission" /d%~dp0  cmd /C cscript.exe ^"%~dp0ListPermissionsForAllShares-Basic-and-Detailed.vbs^" %Audit_Device% ^>^"%~dp0%Audit_Device%_Share_Permissions.txt^"


来源:https://stackoverflow.com/questions/9384044/redirect-output-from-a-command-within-a-batch-file-whose-output-is-already-redir

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!