Ubuntu – Run multiple instance of a program in parallel

bashcommand line

I have a command, let say php process.php. I want to run 1000 instance of this command to test load caused on the server. How can I do this?

Things to note:

  • Ctrl+C should terminate all instance at once

  • It is going to run on a single core processor so no need of advance feature like multi-threading.

  • Nice to show current number of running instance on top

  • Commands should output in the same terminal

  • Commands requires no input

Best Answer

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 the SIGINT 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 using SIGINT once it has tidied up. Hence, the kill -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 from seq, 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