2

I have 50 php files that I would like to run simultaneously from the command line. Right now, I am running them in multiple CLI windows using the code:

php Script1.php

I would like to be able to call one single script file that would execute all 50 php files simultaneously. I have been reading about how to make the command line not wait for the output, but I can't seem to make it work.

I am new to both MAC and Scripting - maybe I don't need a script? Is there another mac based solultion that can do this without me having to open 50 separate terminal windows?

4
  • Do they need to run simultaneously, or is it sufficient that the next one will start when the previous one ended? Commented Mar 27, 2012 at 1:55
  • 1
    Just put an & on the end of the line. Commented Mar 27, 2012 at 1:56
  • They do need to run simultaneously. The script I am running is based on each state. To process it in series takes about 12 hours, and I need to cut that down - 50 times less to be exact :) I was able to run them simultaneously by opening up multiple windows, but I am trying to simply that process. Commented Mar 27, 2012 at 2:12
  • Sorry, I could have been more clear - each US state Commented Mar 27, 2012 at 4:08

3 Answers 3

5

You can just add ampersand '&' to separate each command:

php script1.php & php script2.php & php script3.php ...

This ampersand symbol will tell the shell to run command on background.

To check the output, you can redirect it to a file:

php script1.php > script1.log.txt & php script2.php > script2.log.txt

And you can just do a tail on it to read the log:

tail -f script1.log.txt
Sign up to request clarification or add additional context in comments.

2 Comments

How would I then interact with the script after it is in the background? Know if it started or stopped correctly or abruptly? thanks for the help so far!
the easiest way is to redirect it's output to a file, and check it.
1

If you script is nicely numbered from 1 to 50, you can try the following in a .command file:

i=1;
while [ $i -lt 51 ]
do
osascript -e 'tell app "Terminal"
    do script "php Script$i.php"
end tell' &
i=$[$i+1]
done

This should open 50 separate terminal windows each running script{$i}.php

8 Comments

Andreas, thanks for this. It's very helpful if I wanted to open these windows simultaneously. I ran the script and each window opened, but it just said ' .php cannot be found.' What type of scripting language is this? Can you recommend a link to learn more?
This is actually mac .command file which you can run by double clicking it or running it from your Terminal stackoverflow.com/questions/989349/…
Make sure when you run that you are in the directory where Script1-50 are
Nifty, could you please clarify your script? I copied and pasted, but it wont print the variable $i value in Script$i.php. It just repeats 'Script$i.php' over and over instead of printing out the script value. Thanks!
I don't have a mac handy at the moment.. That script is supposed to open a new terminal for each Script$i.php, do you see that behavior?
|
1

You could also run them at the same time but not in the background.

php test1.php; php test2.php;

I don't know why you would want to "interact" with the script after its running.

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.