Gnu Parallel: While Loop with Sequential Commands
I Need to Run a Bash Script on a List of Subjects. I Would Like to Build a While Loop, That Runs on Multiple Subjects at the Same Time. However, the Commands...
I need to run a bash script on a list of subjects. I would like to build a while loop, that runs on multiple subjects at the same time. However, the commands within the loop must run sequentially.
May you please confirm me that a sintax like this would do the job?
file="subjects.txt"
foo () {
command $subj etc
command 2 $subj etc
command 3 $subj etc
}
while read subj; do foo "$subj" &
done <$file
Thank you in advance for your help.
Ramtin
1 Answer
file="subjects.txt"
foo () {
subj="$1"
command $subj etc
command 2 $subj etc
command 3 $subj etc
}
export -f foo
parallel foo :::: "$file"
It will run n jobs in parallel (where n = number of CPU threads). To change that use parallel -j10 for 10 jobs in parallel.
Read chapter 1+2 of (can be downloaded at ). Your command line will love you for it.