【问题标题】:bash aws commands do not wait until completionbash aws 命令不等到完成
【发布时间】:2021-06-09 16:42:25
【问题描述】:

我在本地机器上有一个 bash 脚本,它发送一系列命令以在终止之前在 ec2 上运行一些脚本。一段代码如下:

cat ec2-commands.sh
aws ssm send-command --document-name "AWS-RunShellScript" --instance-ids "${instanceid}" --parameters commands=["cd /home/ec2-user","aws s3 cp s3://test/scripts/run_model1.sh ."]
aws ssm send-command --document-name "AWS-RunShellScript" --instance-ids "${instanceid}" --parameters commands=["sudo bash /home/ec2-user/run_model1.sh"]
aws ec2 terminate-instances --instance-ids "${instanceid}"

“run_model1.sh”脚本大约需要 3 小时才能完成。但是,在本地机器上,在终止实例之前无需等待“run_model1.sh”完成。换句话说,本地机器发送命令以运行“run_model1.sh”,并且在 ec2 实例开始运行脚本后,这将被视为“成功”发送命令,并且本地脚本发送命令以终止实例执行后不久.我怎样才能有某种“观看”或“等待”,以便脚本看起来像下面这样:

cat ec2-commands.sh
aws ssm send-command --document-name "AWS-RunShellScript" --instance-ids "${instanceid}" --parameters commands=["cd /home/ec2-user","aws s3 cp s3://test/scripts/run_model1.sh ."]
aws ssm send-command --document-name "AWS-RunShellScript" --instance-ids "${instanceid}" --parameters commands=["sudo bash /home/ec2-user/run_model1.sh"]
[command here to wait for 'run_model1.sh' on the ec2 instance to finish]
aws ec2 terminate-instances --instance-ids "${instanceid}"

或者是否有一个带有 ssm send-command 的选项,它会等到脚本在 ec2 实例上完成?

【问题讨论】:

    标签: linux bash amazon-web-services amazon-s3 amazon-ec2


    【解决方案1】:

    您可以使用this 等待命令完成:

    aws ssm wait command-executed
    

    但是有 100 秒的硬性限制,远低于您所说的脚本执行所需的 3 小时。

    为什么不只发送一个命令来完成实例需要做的所有事情,然后在末尾添加一个shutdown -h now 命令?


    否则,您可能会subscribe to SSM Run Command status events,并让类似 Lambda 函数的东西发送下一个命令。

    【讨论】:

    • 这需要 awscli 1.19+
    猜你喜欢
    • 1970-01-01
    • 2011-02-19
    • 1970-01-01
    • 1970-01-01
    • 2013-09-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-20
    • 2012-07-19
    相关资源
    最近更新 更多