Amazon web services 如何对aws cli中“列出任务”的结果进行排序?
我有几个正在运行的任务,我想获得最新的任务(最后开始的任务,最后创建于)任务arn。 我正在使用这个命令Amazon web services 如何对aws cli中“列出任务”的结果进行排序?,amazon-web-services,deployment,aws-cli,amazon-ecs,Amazon Web Services,Deployment,Aws Cli,Amazon Ecs,我有几个正在运行的任务,我想获得最新的任务(最后开始的任务,最后创建于)任务arn。 我正在使用这个命令 aws ecs列表任务--cluster{cluster}--family{family}--所需状态“STOPPED” 经过一点探索,我了解了sort\u by。我试过了 aws ecs列表任务--cluster{cluster}--family{family}--所需状态“STOPPED”--查询“排序依据(taskArns,&CreatedAt)” 但这会产生错误 In funct
aws ecs列表任务--cluster{cluster}--family{family}--所需状态“STOPPED”
经过一点探索,我了解了sort\u by
。我试过了
aws ecs列表任务--cluster{cluster}--family{family}--所需状态“STOPPED”--查询“排序依据(taskArns,&CreatedAt)”
但这会产生错误
In function sort_by(), invalid type for value: <some_task_arn>, expected one of: ['string', 'number'], received: "null"
在函数sort_by()中,值的类型无效,应为:['string','number']之一,收到:“null”
假设您使用Linux/Unix Shell,我建议将输出发送到sort命令。这通过添加
|排序
在命令行末尾执行以下操作:
aws ecs list-tasks --cluster {cluster} --family {family} --desired-status 'STOPPED' | sort
我希望这会有所帮助。
列出任务
不会提供这些信息。必须同时使用列出任务
和描述任务
我将在集群上提供示例,其中正在运行
状态任务。您必须根据自己的需要调整它
1。获取任务列表
task_arns=$(aws ecs list-tasks --cluster ${cluster[Name]} \
--desired-status 'RUNNING' \
--query 'taskArns' --output text)
echo ${task_arns}
aws ecs describe-tasks --cluster ${cluster[Name]} \
--tasks ${task_arns} \
--query "tasks[] | reverse(sort_by(@, &createdAt)) | [].[createdAt,taskArn]" \
--output table
aws ecs describe-tasks --cluster ${cluster[Name]} \
--tasks ${task_arns} \
--query "tasks[] | reverse(sort_by(@, &createdAt)) | [-1].[taskArn]" \
--output text
应提供任务的ARN列表,例如:
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。获取已排序的任务ARN
task_arns=$(aws ecs list-tasks --cluster ${cluster[Name]} \
--desired-status 'RUNNING' \
--query 'taskArns' --output text)
echo ${task_arns}
aws ecs describe-tasks --cluster ${cluster[Name]} \
--tasks ${task_arns} \
--query "tasks[] | reverse(sort_by(@, &createdAt)) | [].[createdAt,taskArn]" \
--output table
aws ecs describe-tasks --cluster ${cluster[Name]} \
--tasks ${task_arns} \
--query "tasks[] | reverse(sort_by(@, &createdAt)) | [-1].[taskArn]" \
--output text
应该给出,例如:
----------------------------------------------------------------------------------------------------
| 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。获取最后一个元素排序的任务ARN
task_arns=$(aws ecs list-tasks --cluster ${cluster[Name]} \
--desired-status 'RUNNING' \
--query 'taskArns' --output text)
echo ${task_arns}
aws ecs describe-tasks --cluster ${cluster[Name]} \
--tasks ${task_arns} \
--query "tasks[] | reverse(sort_by(@, &createdAt)) | [].[createdAt,taskArn]" \
--output table
aws ecs describe-tasks --cluster ${cluster[Name]} \
--tasks ${task_arns} \
--query "tasks[] | reverse(sort_by(@, &createdAt)) | [-1].[taskArn]" \
--output text
应提供:
arn:aws:ecs:us-east-1:275795381673:task/0053c603-a6c9-4044-89f5-b0edc8f6de3f
“最后”用什么标准衡量?数组返回表单中的最后一个
列出任务
,或创建的最后一个任务,或停止的最后一个任务?我指的是启动最后一个任务的任务…其创建时间
是最新的。我想按它们在创建的时间对其进行排序。这将根据我猜的名字排序。