4

Suppose I have a PHP script which takes two parameters from command line and just prints them as output.

I want a batch file which runs many of these PHP scripts parallel with different parameters and redirect the output of each script to a different file.

I tried something like

start "" php index.php 1 2 >> tmp1.txt
start "" php index.php 3 4 >> tmp2.txt

It runs them parallel and their outputs are printed into console window. But I want to redirect each output to a file.

What to modify in batch file to get output of index.php redirected to the file?

3
  • 1
    There may be a spacing issue. Try putting >>tmp1.txt and >>tmp2.txt at the beginning of their respective lines instead. Commented Dec 21, 2015 at 15:51
  • Nop. it just had the same result. Commented Dec 21, 2015 at 16:14
  • 2
    start "" cmd /C ^>^> tmp1.txt php index.php 1 2 Commented Dec 21, 2015 at 18:20

1 Answer 1

2

The Microsoft article about using command redirection operators explains the differences on using > or >> or |.

What is wrong with following line?

start "" php index.php 1 2 >>tmp1.txt

This line results in appending output of command start to file tmp1.txt which is not wanted here.

What is the solution?

The redirection operators are no longer interpreted by command processor for command start by escaping the angle brackets with ^, but for php.exe started in a new command process.

start "" php.exe index.php 1 2 ^>^>tmp1.txt
start "" php.exe index.php 3 4 ^>^>tmp2.txt

The disadvantage of the two lines above is that each console window opened for each new process remains open after php.exe finished processing script file index.php.

The solution is using the code provided already by Aacini with starting a new command process with option /C to close the command process and its console window automatically after php.exe terminated.

start "" cmd.exe /C php.exe index.php 1 2 ^>^>tmp1.txt
start "" cmd.exe /C php.exe index.php 3 4 ^>^>tmp2.txt

The redirection can be also specified left of application which output is redirected and appended to the specified file.

start "" cmd.exe /C ^>^>tmp1.txt php.exe index.php 1 2
start "" cmd.exe /C ^>^>tmp2.txt php.exe index.php 3 4
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.