簡體   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