linux - Wait inside loop bash -
i have created loop read through .txt , execute shell using each line input.
problem: need loop execute first 2 lines txt in parallel, wait them finish, execute next 2 lines in parallel.
attempt @ solution: thought of adding in wait command, not sure how structure waits every 2 lines, opposed each churn of loop.
my current loop:
cat input.txt | while read line; 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
normally you'd use sem, xargs or parallel parallelize loop, these tools optimize throughput having 2 (or n) jobs running in parallel, , starting new ones old ones finish.
to instead run pairs of jobs , wait both finish before considering starting more, can run them in background , keep counter wait every n iterations:
printf "%s\n" {1..10} | while ifs= read -r line echo "starting command" sleep 2 & if (( ++i % 2 == 0 )) echo "waiting..." wait fi done output:
starting command starting command waiting... starting command starting command waiting...
Comments
Post a Comment