Linux shell模拟多线程拷贝

#!/bin/sh
#日志文件
log_file=/data/threadcp.log a=$(date +%H%M%S)
echo -e "startTime:\t$a" #最大线程数
nParellel= #获取需拷贝文件数组
cd /data/testsrc
filelist=()
j=
for file in $(ls *)
do
filelist[j]=$file
let "j++"
done #获取拷贝文件数
filenum=$(ls |wc -w) aJobs=() for ((nextJob = ; nextJob < filenum; )); do
for ((iJob = ; iJob < nParellel; ++iJob)); do
if [ $nextJob -eq $filenum ]; then
break;
fi
if [ ! "${aJobs[iJob]}" ] || ! kill - ${aJobs[iJob]} > /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 . done wait
b=$(date +%H%M%S)
echo -e "endTime:\t$b"
 
上一篇:jQuery源码笔记——延迟对象


下一篇:Shell简单实现多线程