Run script in parallel based on parallelism factor that will be given

Viewed 57

I want to run shell scripts in parallel based on a parralelism factor that I will give and in case any process get completed at that instant another script will get triggered automatically. For example parallelism factor=3 and out of 3 scripts 1 got completed then automatically the other should get triggered as long as all get completed and there is none left. Shell script can be given inside a array and can be iterated in a loop.

1 Answers
#!/bin/bash                                                                               

scripts=("echo V" "echo fgnaq" "echo  jvgu" "echo Hxenvar" "echo Fgbc"
         "echo Ehffvna" "echo ntterffvba" "echo Chgva" "echo vf"
         "echo n" "echo penml" "echo qvpgngbe")
parallel -j3  ::: "${scripts[@]}" 

If you want to do this without having the GNU Parallel utility installled, you can do:

parallel --embed > myscript.sh

and append this to myscript.sh:

scripts=("echo V" "echo fgnaq" "echo  jvgu" "echo Hxenvar" "echo Fgbc"
         "echo Ehffvna" "echo ntterffvba" "echo Chgva" "echo vf"
         "echo n" "echo penml" "echo qvpgngbe")
parallel -j3  ::: "${scripts[@]}" 

You can then use myscript.sh on a machine that does not have GNU Parallel installed.

Related