Home > OS >  Can you run the same loop simultaneously 3 times in a single bash script?
Can you run the same loop simultaneously 3 times in a single bash script?

Time:08-11

If you could just point me in the right direction I'm sure I could figure it out eventually.

I have a loop that works from i=1 to i=75. But I am able to run this a maximum of 3 times concurrently so all I could figure out is running it in 3 separate terminals as i=1 to i=25, i=26 to i=50, and i=51 to i=75.

Ideally I would want it run in the same script and for all 3 instances of the loop simultaneously and to begin at i=1 and end at i=75, however without overlapping i's which have been completed or are being completed by a different instance of the loop.

Example processing:

  • Loop1 begins i=1
  • Loop2 skips i=1, Loop2 begins i=2
  • Loop3 skips i=1,2, Loop3 begins i=3
  • Loop2 completes i=2, Loop2 skips i=3, Loop2 begins i=4
  • Loop3 completes i=3, Loop3 skips i=4, Loop3 begins i=5
  • Loop1 completes i=1, Loop1 skips i=2,3,4,5 Loop1 begins i=6
  • Loop1 completes i=6, Loop1 begins i=7
  • Loop3 completes i=5, Loop3 skips i=7, Loop3 begins i=8

the structure of the loop is basically like this

u=0
while [ $u -lt 1000 ]
do
    ((u  ))
    i=$1
    while [ $i -le $2 ]
    do
        curl #(that sends begining command $i)
        sleep 60
        variable1=$(curl $i) #command that outputs a number which can change every 60 seconds
        variable2=$(curl $i) #command that outputs a number which is static
        counter=0 
        until [ variable1 -ne $variable2 ]
        do
            ((counter  ))
            echo waiting 60 seconds 
            echo times waited: $counter
            sleep 60
            variable1=$(curl $i) #command that outputs a number which can change every 60 seconds
            variable2=$(curl $i) #command that outputs a number which is static
        curl #(that sends ending command $i)
        ((i  ))
        done
done

CodePudding user response:

I provide a simple way to accomplish this, and I let you to customize it per your needs:

#!/bin/bash

function myLoopFunction {
  param1 = $1
  param2 = $2

  # Some code...
}

myLoopFunction value1a value2a & # Run 1
myLoopFunction value1b value2b & # Run 2
myLoopFunction value1c value2c & # Run 3

wait # Wait for termination...

CodePudding user response:

N=3
for (( i = 0; i < N; i   ))
do ( 
    switch $i in
       0) setup_loop_0 ;;
       1) setup_loop_1 ;;
       2) setup_loop_2 ;;
    esac

    echo any commands
    sleep 2
    echo finish
)& done
wait

Notes:

  • N=3 defines the number of parallel executions.
  • for (( i = 0; i < N; i )); do ... done for loop like in other languages
  • You can write (( i = 1; i <= N; i )) instead of (( i = 0; i < N; i )) to get values 1..3 instead 0..2 for variable i.
  • ( ... ) declares a sub-shell.
  • The & behind ) starts the sub-shell in the background
  • wait waits until all sub-shells are terminated.
  • Related