简体   繁体   English

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

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

I'm trying to kill a task in ECS via the CLI.我正在尝试通过 CLI 终止 ECS 中的任务。

I can fetch the task name by executing:我可以通过执行来获取任务名称:

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

which outputs:输出:

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

the full ARN of the task as a string (I have a global defaulting output to JSON).任务的完整 ARN 作为字符串(我有一个全局默认 output 到 JSON)。

I can kill the task by executing:我可以通过执行以下命令终止任务:

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

However when I try and combine it:但是,当我尝试结合它时:

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

I get:我得到:

An error occurred (InvalidParameterException) when calling the StopTask operation: taskId longer than 36.调用 StopTask 操作时出错(InvalidParameterException):taskId 长于 36。

I know this is probably bash program output/argument input interpolation but I've looked that up and cannot get to the bottom of it.我知道这可能是 bash 程序输出/参数输入插值,但我已经查过了,无法深究。

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

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

Maybe that helps someone:也许这有助于某人:

Killing task with unique task definition name:具有唯一任务定义名称的终止任务:

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}

Killing multiple tasks (same task definition name but different task ids):杀死多个任务(相同的任务定义名称但不同的任务 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

One-liner command to stop tasks in cluster/service停止集群/服务中任务的单行命令

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;

One-liner version of nathanpecks great answer: 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