2

I'm having a shell script as follows.

#!/bin/bash myfunc() { #do something (call a rest service) sleep 300 status=$(get status of the operation performed above) while [ "$status" != "succeeded" ]; do #do something (call a rest service) sleep 300 status=$(get status of the operation performed above) done } a=0 while [ $a -lt 1000 ] do a=`expr $a + 1` myfunc done 

In best case, above script takes at least 5*1000=5000 seconds to complete running.

Is there a way to make the myfunc call parallel so that we can let the while loop to spawn multiple running instances of the myfunc??

Note: Somehow, this script should wait until all instances of myfunc executions are completed.

3
  • I am not sure if bash does multi threading. You might wanna use some more sophisticated language than shell script to write this ... Commented Jun 9, 2021 at 14:20
  • export -f myfunc; seq 1000 | xargs -P0 bash -c myfunc Commented Jun 9, 2021 at 14:22
  • 1
    To avoid output from jobs mixing, you can iuse GNU Parallel: export -f myfunc, run parallel myfunc ::: {1..1000} Commented Jun 13, 2021 at 10:10

2 Answers 2

3

Put them in background.

while [ $a -lt 1000 ] do echo $a a=`expr $a + 1` myfunc & done wait 

Or, in more idiomatic bash,

while (( a < 1000 )) do echo $((a++)) myfunc & done wait 
Sign up to request clarification or add additional context in comments.

Comments

2

I think you could update your script as follow:

#!/bin/bash myfunc() { job_id=$1 echo "Started job ${job_id}..." #do something (call a rest service) sleep 300 status=$(get status of the operation performed above) while [ "$status" != "succeeded" ]; do #do something (call a rest service) sleep 300 status=$(get status of the operation performed above) done echo "Terminated job ${job_id}." } a=0 while [ $a -lt 1000 ] do echo $a a=(($a + 1)) myfunc $a & done wait echo "Parallel execution terminated." 

3 Comments

a=($a + 1) that's a typo?
Sorry, it is a typo, I fixed it.
@AntonioPetricca sorry, I kept it as it is and asked a new question. stackoverflow.com/questions/67923291/… can you check this as well?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.