Search code examples
bashshellprocessbackground-process

Running multiple processes through shell script


I have a server executable myserver. I have to start 5 instances of it with different IP addresses (provided through commandline). This is what I have tried:

for i in `seq 1 5`
do
    ip="127.0.0.$i"
    myserver $ip
done

The problem is that after starting first myserver, the control does not come to the terminal. How can I start the processes in background?

Appending with & i.e. myserver $ip & did not help. Most likely, I am doing something wrong with the last & that is used to run processes in background.

Edit: myserver has to continuously listen to a socket bound to its IP and a default port. Therefore, it has an infinite loop inside it.


Solution

  • for i in {1..5}
    do
        ip="127.0.0.$i"
        myserver "$ip" &
    done
    
    wait # for all backgrounded jobs to finish