并行rsync

#!/bin/bash

if [ $# -le 2 ]; then
    echo -e "usage  : \n\t$0 hostList src_file dst_path"
    echo -e "example: \n\t$0 hostList file.txt /home/work/opdir"
    exit 1
fi

# concurrent number by default.
CON_NUM=9
host_list=$1
shift

src=''
while [ $# -gt 1 ]; do
    src="$src $1"
    shift
done
dst=$1

echo "src: $src"
echo "det: $dst"


if [ ! -f $host_list ]; then
    echo "$host_list is not exist!"
    exit 2
fi

function your_cmd() {
    host=$1
    #echo -e "---- cmd() start to : $host --"
    CMD="scp -r ${src} ${host}:${dst} 2>/dev/null"
    eval ${CMD}
    ret=$?
    if [ $ret -ne 0 ];then
        echo -e "\t$host: cmd return retry $ret"
    fi
}

function concurrent()
{
    # ff_file which is opened by fd 4 will be really removed after script stopped
    mkfifo   ./fifo.$$ &&  exec 4<> ./fifo.$$ && rm -f ./fifo.$$

    # initial fifo: write $con_num line to $ff_file
    for ((i=1; i<=$CON_NUM; i++)); do
        echo "init:$i" >&4  # init fifo
    done

    for host in $(cat $host_list | grep -v "^#"); do
        read -u 4   # read from fifo to REPLY
        {   
            #echo -e "\n-- current loop for: \"$host\" [ fifo id: \"$REPLY\" ]"
            your_cmd  "$host"
            echo -e "---- done: $host ----\n"
            echo "real:$host"  1>&4 # write to $fifo
        } & # & to backgroud each process in {}
    done
    wait    # wait all con-current cmd in { } been running over
}

concurrent 

 

转载于:https://www.cnblogs.com/tengfei520/p/7843868.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值