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?

user658182
  • 2,148
  • 5
  • 21
  • 36
  • Do they need to run _simultaneously_, or is it sufficient that the next one will start when the previous one ended? – sarnold Mar 27 '12 at 01:55
  • 1
    Just put an `&` on the end of the line. – David Schwartz Mar 27 '12 at 01: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. – user658182 Mar 27 '12 at 02:12
  • Sorry, I could have been more clear - each US state – user658182 Mar 27 '12 at 04:08

3 Answers3

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
ariefbayu
  • 21,849
  • 12
  • 71
  • 92
  • 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! – user658182 Mar 27 '12 at 02:13
  • the easiest way is to redirect it's output to a file, and check it. – ariefbayu Mar 27 '12 at 02:17
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

Andreas Wong
  • 59,630
  • 19
  • 106
  • 123
  • 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? – user658182 Mar 27 '12 at 04:32
  • This is actually mac .command file which you can run by double clicking it or running it from your Terminal http://stackoverflow.com/questions/989349/running-a-command-in-a-new-mac-os-x-terminal-window – Andreas Wong Mar 27 '12 at 04:46
  • Make sure when you run that you are in the directory where Script1-50 are – Andreas Wong Mar 27 '12 at 04:46
  • 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! – user658182 Mar 31 '12 at 12:07
  • 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? – Andreas Wong Mar 31 '12 at 13:04
  • ah, I got it, thank you. it was an issue with how it was quoted. Working great now! – user658182 Apr 01 '12 at 00:37
  • @user658182 glad to hear that, mind telling me which quote was wrong? I would like to edit my answer :) – Andreas Wong Apr 01 '12 at 00:38
  • I changed osascript -e 'tell app "Terminal" do script "php Script$i.php" end tell' to osascript -e "tell app \"Terminal\" do script \"php Script$i.php\" end tell" – user658182 Apr 01 '12 at 01:19
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.