Reputation: 33
I have created a loop to read through a .txt
and execute another shell using each line as input.
PROBLEM: I need the loop to execute the first two lines from the txt in parallel, wait for them to finish, then execute the next two lines in parallel.
ATTEMPT AT SOLUTION: I thought of adding in a wait command, just not sure how to structure so it waits for every two lines, as opposed to each churn of the loop.
My current loop:
cat input.txt | while read line; do
export step=${line//\"/}
export step=ExecuteModel_${step//,/_}
export pov=$line
$owsdirectory"/hpm_ws_client.sh" processCalcScriptOptions "$appName" "$pov" "$layers" "$stages" "" "$stages" "$stages" FALSE > "$appLogFolder""/"$step"_ProcessID.log"
/app/dev2/batch/hpcm/shellexe/rate_tool2/model_automation/check_process_status.sh "$appLogFolder" "$step" > "$appLogFolder""/""$step""_Monitor.log"
Input txt:
SEQ010,FY15
SEQ010,FY16
SEQ020,FY15
SEQ020,FY16
SEQ030,FY15
SEQ030,FY16
SEQ030,FY15
SEQ030,FY16
SEQ040,FY15
SEQ040,FY16
SEQ050,FY15
SEQ050,FY16
Upvotes: 1
Views: 9327
Reputation: 123410
Normally you'd use sem
, xargs
or parallel
to parallelize a loop, but all these tools optimize throughput by always having 2 (or N) jobs running in parallel, and starting new ones as old ones finish.
To instead run pairs of jobs and wait for both to finish before considering starting more, you can just run them in the background and keep a counter to wait
every N iterations:
printf "%s\n" {1..10} | while IFS= read -r line
do
echo "Starting command"
sleep 2 &
if (( ++i % 2 == 0 ))
then
echo "Waiting..."
wait
fi
done
Output:
Starting command
Starting command
Waiting...
Starting command
Starting command
Waiting...
Upvotes: 3