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
):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.SIGINT
kill itself usingSIGINT
once it has tidied up. Hence, thekill -INT $$
.seq 1000
generates numbers from 1 to 1000.while
loop runs the command provided as arguments to the script once for each number fromseq
, and sends them to the background.wait
until all of the background processes finish executing.Run it thus: