并行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 
复制代码

 

posted @   tengfei520  阅读(576)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示