码迷,mamicode.com
首页 > 其他好文 > 详细

并行cmd

时间:2017-11-16 14:08:56      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:been   eva   default   write   done   current   $?   ash   running   

#!/bin/bash

if [ $# -le 1 ]; then
    echo -e "usage  : \n\t$0 hostList REMOTE_CMD"
    echo -e "example: \n\t$0 hostList \"docker images | grep hive\""
    exit 1
fi

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

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="env TERM=xterm ssh $host \"$remote_cmd\" 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
}

 

并行cmd

标签:been   eva   default   write   done   current   $?   ash   running   

原文地址:http://www.cnblogs.com/tengfei520/p/7843862.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!