Run a shell script in a loop with max number of threads

hi guys. i have a question for you

i have a one file and inside this file there are 1000 lines and each line is a linux command

running this commands takes long time so i want to create one bash script and run this lines in a loop with max number of threads

for example i want to run only 5 lines from this file once one of this command finished run the next (6th) command in a loop. how can i do that?

Thanks in advance

Welcome to the forum.

Please become accustomed to provide decent context info of your problem.
It is always helpful to carefully and detailedly phrase a request, and to support it with system info like OS and shell, related environment (variables, directory structures, options), preferred tools, adequate (representative) sample input and desired output data and the logics connecting the two including your own attempts at a solution, and, if existent, system (error) messages verbatim, to avoid ambiguities and keep people from guessing.

I'm afraid you can't create threads with shell means, but processes, and you should not expect too much performancewise when the processes you run in parallel compete for system resources.

There are several threads in these forums dealing with "parallel execution"; try searching for those. Or try sth. along this line (recent shell like bash needed):

while read CMD; do . <(echo "$CMD") & (( ++CNT%5 )) && wait; done < file

EDIT: MadeInGermany showed an interesting approach here

2 Likes

thank you so much