Run multiple instance of a program in parallel
You could use a script. Save the following as run-parallel.sh
, and make it executable (chmod +x run-parallel.sh
):
#! /bin/bash
trap "pkill -P $$; kill -INT $$" INT
while read n
do
"$@" &
done < <(seq 1000)
wait
Explanation:
-
trap ... INT
sets a command to be run when theSIGINT
signal is received (which is the signal generated when you press CtrlC). -
$$
is the PID of the current process (that is, of the script itself).pkill -P $$
kills those processes whose parent process i the script. - It is convention that a program which catches
SIGINT
kill itself usingSIGINT
once it has tidied up. Hence, thekill -INT $$
. -
seq 1000
generates numbers from 1 to 1000. - The
while
loop runs the command provided as arguments to the script once for each number fromseq
, and sends them to the background. - Then we
wait
until all of the background processes finish executing.
Run it thus:
./run-parallel.sh php process.php
You can use xargs
. Just print the name of the process you want 100 times and pass it to xargs
:
perl -e 'print "/path/to/process.php\n" x 100' | xargs -P 100 -I {} php {}
The Perl command simply prints /path/to/process.php
followed by a newline 100 times. This is then passed to xargs
which is told to launch up to 100 parallel commands (-P 100
). The -I {}
tells xargs
to replace the string {}
with its input. Therefore, php {}
is expanded to php /path/to/process.php
. Since everything is done by xargs
, a single Ctrl+C will kill all of them, and all output is printed to the same terminal.