5

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

3 Answers 3

8

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
8

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.

0

You can write a Master program to start 1000 times php process.php. Using fork and exec may be convenient if you use C. When the Master starts a new php process, it also records its pid.

Show current number of running instance: the Master can monitor the status of the subprocesses.

Ctrl+C should terminate all instance at once: the Master captures Ctrl+C signal, and kills all the subprocesses (kill(pid, SIGKILL);) or notifies them to exit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.