如何并行bash for循环 如何使这个循环使用并行linux命令? func() { ssh -q $cn "systemctl restart ntpd" } for num in $(seq 30 40) ; do num_tmp=$(printf "%03d" $num) cn="node$num_tmp" func done | parallel

如何并行bash for循环 如何使这个循环使用并行linux命令? func() { ssh -q $cn "systemctl restart ntpd" } for num in $(seq 30 40) ; do num_tmp=$(printf "%03d" $num) cn="node$num_tmp" func done | parallel,bash,gnu-parallel,Bash,Gnu Parallel,func函数根据需要进行了更改,基本上它可以登录到节点并在那里运行命令 正如我所看到的,我试图在循环的末尾添加parallel,但它不起作用将这个“&”添加到func的末尾 ..... cn="node$num_tmp" func & done 也许像这样: seq -f "%03G" 30 40 | parallel --dry-run 'ssh -q node{} "systemctl restart nt

func函数根据需要进行了更改,基本上它可以登录到节点并在那里运行命令

正如我所看到的,我试图在循环的末尾添加parallel,但它不起作用

将这个“&”添加到func的末尾

 .....
    cn="node$num_tmp"    
    func &
done
也许像这样:

seq -f "%03G" 30 40 | parallel --dry-run 'ssh -q node{} "systemctl restart ntpd"'
样本输出

ssh -q node030 "systemctl restart ntpd"
ssh -q node031 "systemctl restart ntpd"
ssh -q node032 "systemctl restart ntpd"
ssh -q node033 "systemctl restart ntpd"
ssh -q node034 "systemctl restart ntpd"
ssh -q node035 "systemctl restart ntpd"
ssh -q node036 "systemctl restart ntpd"
ssh -q node037 "systemctl restart ntpd"
ssh -q node038 "systemctl restart ntpd"
ssh -q node039 "systemctl restart ntpd"
ssh -q node040 "systemctl restart ntpd"
或者,如果您有bash v4或更新版本,它可以为您设置零焊盘:

parallel --dry-run 'ssh -q node{} "systemctl restart ntpd"' ::: {030..040}

您没有显示
func
的代码,而且设计也不是最优的,因为它可能使用了一个全局变量。你能重铸你的
func
来获取一个参数,这样它就更适合并行化了吗?