(I have searched and expected this question to have been asked before but couldn't find anything like this although there are plenty of similar questions)
I want this for-loop to run in 3 different threads/processes and wait seem to be the right command
for file in 1.txt 2.txt 3.text 4.txt 5.txt
do something lengthy &
i=$((i + 1))
wait $!
done
But this construct, I guess, just starts one thread and then wait until it is done before it starts the next thread. I could place wait outside the loop but how do I then
- Access the pids?
- Limit it to 3 threads?
waitis to make sure that nothing else happens before all five have exited?waita PID. If you callwaitwith no arguments it will wait on all background processes, so putting thewaitafterdonewill wait for all threads to complete. Not sure how to limit to 3 threads though...bashby itself isn't really suitable for maintaining a process pool like this.