#!/bin/sh #日志文件 log_file=/data/threadcp.log a=$(date +%H%M%S) echo -e "startTime: $a" #最大线程数 nParellel=4 #获取需拷贝文件数组 cd /data/testsrc filelist=() j=0 for file in $(ls *) do filelist[j]=$file let "j++" done #获取拷贝文件数 filenum=$(ls |wc -w) aJobs=() for ((nextJob = 0; nextJob < filenum; )); do for ((iJob = 0; iJob < nParellel; ++iJob)); do if [ $nextJob -eq $filenum ]; then break; fi if [ ! "${aJobs[iJob]}" ] || ! kill -0 ${aJobs[iJob]} 2> /dev/null; then { cp ${filelist[nextJob]} /data/extd echo "[$(date "+%F %T")]: ${filelist[nextJob]}" >> $log_file # echo "[$(date "+%F %T")]: ${nextJob}" >> $log_file } & let "nextJob++" aJobs[iJob]=$! fi done sleep .1 done wait b=$(date +%H%M%S) echo -e "endTime: $b"