Bash & Shell Scripting (50 Qs)•Part 3: Advanced Text Processing & Production Internals (Senior / Lead)
Concurrency and Parallel Job Processing in Bash: Background jobs (`&`), `wait`, and `xargs -P`.
senior LevelCode Examplebash-shellAdvanced Text Processing
In Simple Words: Executing independent tasks in parallel accelerates batch processing:
bash
# 1. Background Jobs and wait
pids=()
for file in data_*.csv; do
process_file "$file" &
pids+=($!)
done
# Wait for all background jobs to finish and capture errors
for pid in "${pids[@]}"; do
wait "$pid" || echo "Process $pid failed"
done
# 2. Using xargs with parallel workers (e.g. 8 parallel cores)
find ./images -name "*.png" | xargs -P 8 -I {} pngquant --ext -min.png {}