2017-03-04 77 views
0

我有一个shell脚本来下载一批文件并希望控制它具有一定数量的后台下载作业。如何控制shell脚本中后台作业的数量

+0

恕我直言,这可能是最好不要重新发明轮子,用** GNU并行**。它的骨骼是'parallel -j6 -a $ json wget {}' –

回答

0
json='list.json' 
wget -o $json http://fileserver/getFileList 

jobsCount=0 
while IFS= read -r url ; do 

     #download the file 
     wget -q $url & 

     #contorling the amount of concurrent jobs 
      while true; do 
      #get current background job count 
      jobsCount=$(jobs -p | wc -l) 

      if [ $jobsCount -lt 6 ]; then 
       echo "jobsCount $jobsCount" 
       break 
      fi 
      echo "jobsCount $jobsCount (Sleep)" 
      sleep 1 
      done 

done < <(jq -r '.data[] | (.URL)' <$json) 
#wait for last batch of job to finish 
wait 
相关问题