Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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...

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading