Forum Moderators: coopster

Message Too Old, No Replies

Executing system commands in 'parallel'

Want to execute 50 system commands with a loop in PHP

         

physics

5:08 am on Jul 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Basically I just want to be able to execute an external program 50 times (a perl program on a linux system) from within a PHP script and have the jobs all start 'right away'. I'm using exec in a loop now and what's happening is that the one to finish before the next one starts. This isn't good because the programs take a long time to finish and I'd like them to run in parallel.

jatar_k

5:23 am on Jul 14, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



options are

exec
passthru
system

as far as I know they all wait for return, though I was sure one of them isn't supposed to.

I usually use shell scripts to bridge the gap, php script invokes the shell script and the shell script returns quickly

[edited by: jatar_k at 5:23 am (utc) on July 14, 2005]

grandpa

5:23 am on Jul 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



We would run parallel applications on an AS/400 thru multiple job queues. I'm not versed enough in linux to know if you can do something similar. But you can run a batch script that might help out. The PHP command line [us2.php.net] looks like it might be what you need.

marcs

5:39 am on Jul 14, 2005 (gmt 0)

10+ Year Member



No expert on Perl, but from a command shell (or for from the C system() call), you'd do this :

command &

or

sytem("/path/to/command &");

The & makes it so the command to wait until it is done for the script calling it to regain control.

physics

6:07 am on Jul 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi, thanks for the replies. marcs I've tried that but it still seems to want to wait for the job to finish.
I ended up working around:
1) php script uses exec or whatever to call perl script that gets info from database
2) said perl script makes a .sh file with lines (which will execute the desired perl script) with the right command line arguments, ending in &
3) perl script executes .sh file which then rapid-fire starts up 50 of the desired perl scripts

What a pain. Thanks for the suggestions though, I'm sure I'm missing some easier way.

jatar_k

6:52 am on Jul 14, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



that's about the same way I have had to do a few things in the past, fun ;)