bash控制任务并发数脚本
bash控制任务并发数脚本使用bash实现控制任务并发数的功能01#!/bin/bash02#0304CMDS[1]="echo $USER' 01 BEGIN' && sleep 6 && echo $USER' 01 END'"05CMDS[2]="echo $USER' 02 BEGIN' && sleep 3 && echo $USER' 02 END'"06CMDS[3]="echo $USER' 03 BEGIN' && sleep 7 && echo $USER' 03 END'"07CMDS[4]="echo $USER' 04 BEGIN' && sleep 4 && echo $USER' 04 END'"08CMDS[5]="echo $USER' 05 BEGIN' && sleep 2 && echo $USER' 05 END'"09CMDS[6]="echo $USER' 06 BEGIN' && sleep 5 && echo $USER' 06 END'"1011N=${#CMDS[@]}1213MAX=314PIDS=''15for I in $(seq $N)16do17 while [ 1 -gt 0 ]18 do19 C=020 pids=''21 for PID in $PIDS22 do23 ps $PID >/dev/null 2>/dev/null24 if [ $? -eq 0 ]25 then26 pids="$pids $PID"27 (( C = C + 1 ))28 fi29 done30 PIDS=$pids3132 [ $C -lt $MAX ] && break33 sleep 134 done3536 #echo $i ${CMDS[$i]}37 CMD=${CMDS[$I]}38 bash -c "$CMD" &39 PIDS="$PIDS $!"40done4142wait