繁体   English   中英

通过 CLI 在 AWS ECS 上停止任务(程序 output 作为参数输入 bash)

[英]Stopping task on AWS ECS via CLI (program output as argument input bash)

我正在尝试通过 CLI 终止 ECS 中的任务。

我可以通过执行来获取任务名称:

aws ecs list-tasks --cluster "my-cluster" --service-name "my-service" | jq .taskArns[0]

输出:

"arn:aws:ecs:REGION:ACCOUNT-ID:task/TASK-GUID"

任务的完整 ARN 作为字符串(我有一个全局默认 output 到 JSON)。

我可以通过执行以下命令终止任务:

aws ecs stop-task --cluster "my-cluster" --task "task-arn"

但是,当我尝试结合它时:

aws ecs stop-task --cluster "my-cluster" --task $(aws ecs list-tasks --cluster "my-cluster" --service-name "my-service" | jq .taskArns[0])

我得到:

调用 StopTask 操作时出错(InvalidParameterException):taskId 长于 36。

我知道这可能是 bash 程序输出/参数输入插值,但我已经查过了,无法深究。

AWS cli 基本上内置了 jq,因此查询任务 arn 的更好(更简单)方法是:

aws ecs list-tasks --cluster "my-cluster" --service "my-service" --output text --query taskArns[0]

也许这有助于某人:

具有唯一任务定义名称的终止任务:

OLD_TASK_ID=$(aws ecs list-tasks --cluster ${ecsClusterName} --desired-status RUNNING --family ${nameTaskDefinition} | egrep "task/" | sed -E "s/.*task\/(.*)\"/\1/")
aws ecs stop-task --cluster ${ecsClusterName} --task ${OLD_TASK_ID}

杀死多个任务(相同的任务定义名称但不同的任务 id):

OLD_TASK_IDS=$(aws ecs list-tasks --cluster ${ecsClusterName} --desired-status RUNNING --family ${nameTaskDefinition} | egrep "task/" | sed -E "s/.*task\/(.*)\"/\1/" | sed -z 's/\n/ /g')
IFS=', ' read -r -a array <<< "$OLD_TASK_IDS"
for element in "${array[@]}"
do
    aws ecs stop-task --cluster ${ecsClusterName} --task ${element}
done

停止集群/服务中任务的单行命令

for taskarn in $(aws ecs list-tasks --cluster ${YOUR_CLUSTER} --service ${YOUR_SERVICE} --desired-status RUNNING --output text --query 'taskArns'); do aws ecs stop-task --cluster ${YOUR_CLUSTER} --task $taskarn; done;

nathanpecks 的单行版本很好的答案:

aws ecs stop-task --cluster "my-cluster" --task $(aws ecs list-tasks --cluster "my-cluster" --service "my-service" --output text --query taskArns[0])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM