Self answers:How to stop all tasks on a cluster with a single cli command, easily allowing for extra parameters to be passed.
Asked
Active
Viewed 314 times
3 Answers
1
I was trying to get Baron's answer to work and found that it needed an adjustment to work in my AWS CodeBuild (Ubuntu) use case by pulling the task id out of the ARN:
aws ecs list-tasks --cluster "${clustername}" | jq -r ".taskArns[] | split(\"/\")[2]" | xargs -n1 aws ecs stop-task --no-cli-pager --cluster "${clustername}" --task
You might also want to stop all tasks for only one service
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

Steve
- 11
- 1
0
The below will:
- Get all the tasks in the cluster
- Select the task arns using
jq
,-r
removes the quotes from the json value. - Pass each arn to the next command using
xargs
, the value is appended to the command (after --task).n-1
just ensures there is one command per arn, not sure if necessary.
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
prevents the output from stop-task
from getting stuck after each execution.
Any optimization welcome. I saw another solution with awk but found it hard to use with passing extra params to the second command.

Baron
- 323
- 4
- 10
0
If you want to do this with Windows Powershell
:
$arns = aws ecs list-tasks --cluster YOUR_CLUSTER_NAME --service YOUR_SERVICE_NAME --query "taskArns" | ConvertFrom-Json
foreach ($arn in $arns) { aws ecs stop-task --no-cli-pager --cluster YOUR_CLUSTER_NAME --task $arn}

Benvorth
- 7,416
- 8
- 49
- 70