使用 AWS CLI 停止所有 ECS 集群任务

问题描述 投票:0回答:2

自我解答:如何使用单个 cli 命令停止集群上的所有任务,轻松允许传递额外参数。

bash amazon-web-services shell amazon-ecs aws-cli
2个回答
0
投票

以下将:

  1. 获取集群中的所有任务
  2. 使用
    jq
    选择任务arns,
    -r
    从中删除引号 json 值。
  3. 使用
    xargs
    将每个arn传递给下一个命令,值为 附加到命令(在 --task 之后)。
    n-1
    只是确保有 每个 arn 一个命令,不确定是否有必要。

aws ecs list-tasks --cluster "$ecs_cluster" | jq -r ".taskArns[]" | xargs -n1 aws ecs stop-task --no-cli-pager --cluster "$ecs_cluster" --task

--no-cli-pager
防止
stop-task
的输出在每次执行后卡住。

欢迎任何优化。我看到了 awk 的另一个解决方案,但发现很难将额外的参数传递给第二个命令。


0
投票

我试图让 Baron 的答案起作用,但发现它需要通过从 ARN 中提取任务 ID 进行调整才能在我的 AWS CodeBuild (Ubuntu) 用例中起作用:

aws ecs list-tasks --cluster "${clustername}" | jq -r ".taskArns[] | split(\"/\")[2]" | xargs -n1 aws ecs stop-task --no-cli-pager --cluster "${clustername}" --task

您可能还想只停止一项服务的所有任务

aws ecs list-tasks --cluster "${clustername}" --service "${servicename}" | jq -r ".taskArns[] | split(\"/\")[2]" | xargs -n1 aws ecs stop-task --no-cli-pager --cluster "${clustername}" --task
© www.soinside.com 2019 - 2024. All rights reserved.