How to make a bash script that creates 40 simultaneous instances of the program?

I am new to bashand Linux. I have a program that I wrote that I want to create several simultaneous instances.

Now I do this by opening 10 new terminals, and then run the program 10 times (the command I run, php /home/calculatedata.php

What is the easiest way to do this with a bash script? Also, I need to know how to kill instances because they start an infinite loop.

Thanks!!

+7
source share
6 answers

You can use a loop and start processes in the background with &:

for (( i=0; i<40; i++ )); do
   php /home/calculatedata.php &
done

PHP, , , - killall

killall php
+7
for instance in {1..40}
do
  php myscript &
done
+5

php- :

#!/bin/bash
for ((i=1;i<=40;i+=1)); do
  php /home/calculatedata.php &
done

PHP, :

killall php

, php, . PHP, - :

ps -ef | grep /home/calculatedata.php | cut_the_pid | kill -9
+4

"&" :

INSTANCES=40
for ((i=0; $i<$INSTANCES; ++i))
do
    mycmd &
done
+1

seq(1) ( , ), , :

for n in $(seq 40); do
   mycmd &
done

n . .

+1

This script is a loop that instantiates the while true variable; do: 'loop. He continues to carry out tasks until it is canceled, and all tasks are performed in the background. You can add variables for the index counter and change: to your code.

while true; do while true; do :; done & done

stop:

killall bash

-1
source

All Articles