简体   繁体   中英

Running multiple instances of a PHP file from bash and APPEND result to file?

I have a php file, called iter.php. I can easily run this from bash typing:

php iter.php >> result.txt

And I can run it multiple times in a row by just repeating it. However, I need to run them concurrently. So, in other words, if I run two instances in a row, they should finish at more or less the same time and output right after each other, as apposed to running right after each other.

Can I accomplish this in bash?

by using & :

php iter.php >> result.txt & php iter.php >> result.txt

You can test this is working by

php iter.php >> result.txt & sleep 10000 & php iter.php >> result.txt
!#/bin/bash

seq $1 | while read num do
    php iter.php >> $2 &
done

The above bash script should run $1 (first script parameter) processes conccurently, and output the result in $2 (second bash parameter).

Note that if your PHP script executes very quickly, you might actually not see the result of a concurrent run but instead a sequential one. It depends on whether:

  • your system is monocore or multicore: you are more likely to get a concurrent result on the later,
  • on monocores, the script execution takes more time than one "cycle" of scheduling, and whether after that cycle, the bash process get hold of the CPU, (assuming that said cycle has a fixed duration, which is not necessarily true),
  • access to the output can be concurrent (all things being equal, shell redirection should allow that though)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM