how to run increasing number of parallel tasks in bash script

Mar 13, 2014 19:33



  1. #!/bin/bash

  2. today=$(date +%Y-%m-%d.%H:%M:%S)

  3. out_file=res_$today.txt

  4. echo 'starting: ' $today > $out_file

  5. #will go 1, 10, 20,..., 50

  6. for ((b=0, c=1; c <= 50; b++, c=b*10 ))

  7. do

  8.       START=$(date +%s)

  9.       echo 'tasks: '$c

  10.       echo 'tasks: '$c >> $out_file

  11.       for ((a=1; a <= c; a++))

  12.       do

  13.               #run the task in silent mode in background:

  14.               sleep 3 > /dev/null 2>&1 &

  15.       done

  16.       wait

  17.       END=$(date +%s)

  18.       DIFF=$(( $END - $START ))

  19.       echo "per-task avg execution time (total: $DIFF seconds):" >> $out_file

  20.       echo $( echo "scale=2;$DIFF/$c" | bc ) >> $out_file

  21. done

  22. echo 'finished: ' $(date +%Y-%m-%d.%H:%M:%S) >> $out_file

bash, programming

Previous post Next post
Up