砸向脚本用户的命令并行,以减少时间

0

的问题

对不起我还是新来砸脚本。 我们周围10000EC2实例,我已经创造了这个庆典脚本来改变我的EC2的实例类型,所有实例中的姓名和类型都存储在一个文件。 代码是工作,但是把这么长的时间通过运行实例的实例。

是否有任何已知道,如果我可以运行用户命令在所有EC2实例中的一种去吗? 谢谢:)

#!/bin/bash

my_file='test.txt'

declare -a instanceID
declare -a fmo #Future Instance Size

while IFS=, read -r COL1 COL2; do

   instanceID+=("$COL1")
   fmo+=("$COL2")   

done <"$my_file"

len=${#instanceID[@]}

for (( i=0; i < $len; i++)); do

   vm_instance_id="${instanceID[$i]}"
   vm_type="${fmo[$i]}"

   echo Stoping $vm_instance_id
   aws ec2 stop-instances --instance-ids $vm_instance_id

   echo " Waiting for $vm_instance_id state to be STOP "
   aws ec2 wait instance-stopped --instance-ids $vm_instance_id


   echo Resizing $vm_instance_id to $vm_type 
   aws ec2 modify-instance-attribute --instance-id $vm_instance_id --instance-type $vm_type
   


   echo Starting $vm_instance_id 
   aws ec2 start-instances --instance-ids $vm_instance_id
    

done
automation aws-automation aws-cli bash
2021-11-23 09:52:30
2

最好的答案

1

"重构"你的代码来一个函数,是通过了一线从文件。

work() {
   IFS=, read -r instanceID fmo <<<"$1"
   stuff "$instanceID" "$fmo"
}

运行GNU xargs或GNU平行每一行文件,呼吁所导出的功能。 使用 -P 选项运行的功能并行,请参阅文件。

export -f work
xargs -P0 -t bash -c 'work "$@"' -- <"$my_file"
2021-11-23 10:03:16
0

作为@KamilCuk指在这里,你可以容易地做这个并行运行。 然而,如果运行这个脚本行,你最终可能得到的限制通过EC2,以确保包括某些退避+试逻辑/尊重的限制规定在这里 https://docs.aws.amazon.com/AWSEC2/latest/APIReference/throttling.html

2021-11-25 06:11:48

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................