简体   繁体   English

如何在 aws-cli 中对“list-tasks”的结果进行排序?

[英]How to sort results from `list-tasks` in aws-cli?

I have few tasks running i want to get the latest(task which started last, with last created-at) task arn.我有几个任务正在运行,我想获得最新的(最后开始的任务,最后创建的)任务 arn。 I am using this command我正在使用这个命令

aws ecs list-tasks --cluster {cluster} --family {family} --desired-status 'STOPPED'

after exploring a little i got to know about sort_by .经过一番探索后,我了解了sort_by I tried我试过了

aws ecs list-tasks --cluster {cluster} --family {family} --desired-status 'STOPPED' --query "sort_by(taskArns, &CreatedAt)"

But this gives error但这会产生错误

In function sort_by(), invalid type for value: <some_task_arn>, expected one of: ['string', 'number'], received: "null"

Assuming you work from a Linux/Unix-Shell I would suggest sending the output to the sort command.假设您使用 Linux/Unix-Shell 工作,我建议将 output 发送到排序命令。 This works by adding这通过添加

| sort

to your command at the end of the line:在行尾的命令:

aws ecs list-tasks --cluster {cluster} --family {family} --desired-status 'STOPPED' | sort

I hope this helps.我希望这有帮助。

list-tasks will not give you that information. list-tasks不会为您提供该信息。 Have to use both list-tasks and describe-tasks .必须同时使用list-tasksdescribe-tasks

I will provide example on my cluster , with RUNNING state tasks.我将在我的集群上提供示例,其中包含RUNNING state 任务。 You will have to adjust it to your need .必须根据需要对其进行调整

1. Get the list of tasks 1.获取任务列表

task_arns=$(aws ecs list-tasks --cluster ${cluster[Name]} \
               --desired-status 'RUNNING' \
               --query 'taskArns' --output text)

echo ${task_arns}

Should give a list of Arns of your tasks, eg:应该给出你的任务的 Arns 列表,例如:

arn:aws:ecs:us-east-1:275795381673:task/0053c603-a6c9-4044-89f5-b0edc8f6de3f arn:aws:ecs:us-east-1:275795381673:task/0b4626ea-0f2b-4c99-9e90-010e8a0c8ad3 arn:aws:ecs:us-east-1:275795381673:task/0d4aa5f2-f547-45ad-b1f8-ed84ef1d678c arn:aws:ecs:us-east-1:275795381673:task/190a320f-4b68-497a-921e-439460447d45 arn:aws:ecs:us-east-1:275795381673:task/c979f4a2-3665-4c56-93c6-e9b88f6b3519

2. Get sorted tasks Arns 2. 获取排序任务 Arns

aws ecs describe-tasks --cluster ${cluster[Name]} \
    --tasks  ${task_arns} \
    --query "tasks[] | reverse(sort_by(@, &createdAt)) | [].[createdAt,taskArn]" \
    --output table

Should give, eg:应该给,例如:

----------------------------------------------------------------------------------------------------
|                                           DescribeTasks                                          |
+----------------+---------------------------------------------------------------------------------+
|  1589888493.15 |  arn:aws:ecs:us-east-1:275795381673:task/c979f4a2-3665-4c56-93c6-e9b88f6b3519   |
|  1589888501.348|  arn:aws:ecs:us-east-1:275795381673:task/190a320f-4b68-497a-921e-439460447d45   |
|  1589888499.438|  arn:aws:ecs:us-east-1:275795381673:task/0d4aa5f2-f547-45ad-b1f8-ed84ef1d678c   |
|  1589888500.312|  arn:aws:ecs:us-east-1:275795381673:task/0b4626ea-0f2b-4c99-9e90-010e8a0c8ad3   |
|  1589888497.701|  arn:aws:ecs:us-east-1:275795381673:task/0053c603-a6c9-4044-89f5-b0edc8f6de3f   |
+----------------+---------------------------------------------------------------------------------+

3. Get last element sorted tasks Arns 3. 获取最后一个元素排序的任务 Arns

aws ecs describe-tasks --cluster ${cluster[Name]} \
    --tasks  ${task_arns} \
    --query "tasks[] | reverse(sort_by(@, &createdAt)) | [-1].[taskArn]" \
    --output text

Should give:应该给:

arn:aws:ecs:us-east-1:275795381673:task/0053c603-a6c9-4044-89f5-b0edc8f6de3f

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

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