4

How can I simultaneously run dozens of PHP scripts in the background from XAMPP's command line interface?

Someone suggested a batch file, but when I tried executing this:

start php 1.php
start php 2.php
start php 3.php

It only opened a command prompt window; I closed that window, then two more command prompt windows opened up executing 2.php and 3.php.

I want to run as many scripts as I want all simultaneously and all in the background. What is the best way to accomplish this, and how can it be done?


EDIT Here is how to accomplish this:

1) Download PowerShell 2 (or the compatible version with your Windows version) from here: http://support.microsoft.com/kb/968929

2) Install it and restart your computer.

3) Make a new file "example.ps1" and save it to a directory of your choice (e.g. c:\examples\example.ps1). Put this code in that file:

start-job {.\c:\xampp\php\php C:\xampp\htdocs\phpfiles\1.php}
start-job {.\c:\xampp\php\php C:\xampp\htdocs\phpfiles\2.php}
start-job {.\c:\xampp\php\php C:\xampp\htdocs\phpfiles\3.php}
start-job {.\c:\xampp\php\php C:\xampp\htdocs\phpfiles\4.php}
start-job {.\c:\xampp\php\php C:\xampp\htdocs\phpfiles\5.php}
start-job {.\c:\xampp\php\php C:\xampp\htdocs\phpfiles\6.php}
start-job {.\c:\xampp\php\php C:\xampp\htdocs\phpfiles\7.php}
start-job {.\c:\xampp\php\php C:\xampp\htdocs\phpfiles\8.php}
start-job {.\c:\xampp\php\php C:\xampp\htdocs\phpfiles\9.php}
start-job {.\c:\xampp\php\php C:\xampp\htdocs\phpfiles\10.php}
start-job {.\c:\xampp\php\php C:\xampp\htdocs\phpfiles\11.php}

4) Click on "Start" -> "Run" and type in "powershell" (without quotes).

5) After PowerShell initiates, type in the following and press enter:

Set-ExecutionPolicy RemoteSigned

Then press "y" to set the changes.

6) Now type in (make sure to enter in the full file path):

c:\examples\example.ps1

7) To see your jobs simply type in:

get-job

To stop a job use this command:

stop-job JobName
Pamela
  • 187
  • 1
  • 13

1 Answers1

1

Add some options after your start:

start /min cmd /c php -f 1.php
...

This will suppress the additional command prompts.

Mike Mackintosh
  • 272
  • 3
  • 12
  • Thanks, but this only minimizes the 3 command prompt windows. Is there a way to run multiple scripts through CLI entirely in the background (in one window only)? – Pamela Aug 30 '12 at 15:48
  • 1
    For that, you will need the POSIX capabilities of PowerShell. Look at this article: http://jtruher.spaces.live.com/blog/cns!7143DA6E51A2628D!130.entry – Mike Mackintosh Aug 30 '12 at 15:55
  • 1
    Thanks! That article is a bit outdated as PowerShell now integrates background jobs. I've updated my question with the full instructions on how to accomplish this. – Pamela Aug 30 '12 at 19:01