Skip to content

stop_task

stop_task(
    task: String,
    *,
    config_overrides: Optional[ECSClientConfig] = None,
    cluster: Optional[String] = None,
    reason: Optional[String] = None,
) -> StopTaskResponse

Stops a running task. Any tags associated with the task will be deleted.

When you call StopTask on a task, the equivalent of docker stop is issued to the containers running in the task. This results in a stop signal value and a default 30-second timeout, after which the SIGKILL value is sent and the containers are forcibly stopped. This signal can be defined in your container image with the STOPSIGNAL instruction and will default to SIGTERM. If the container handles the SIGTERM value gracefully and exits within 30 seconds from receiving it, no SIGKILL value is sent.

For Windows containers, POSIX signals do not work and runtime stops the container by sending a CTRL_SHUTDOWN_EVENT. For more information, see Unable to react to graceful shutdown of (Windows) container #25982 on GitHub.

The default 30-second timeout can be configured on the Amazon ECS container agent with the ECS_CONTAINER_STOP_TIMEOUT variable. For more information, see Amazon ECS Container Agent Configuration in the Amazon Elastic Container Service Developer Guide.

Parameters:

  • cluster (Optional[String], default: None ) –

    The short name or full Amazon Resource Name (ARN) of the cluster that hosts the task to stop. If you do not specify a cluster, the default cluster is assumed.

  • task (String) –

    Thefull Amazon Resource Name (ARN) of the task.

  • reason (Optional[String], default: None ) –

    An optional message specified when a task is stopped. For example, if you're using a custom scheduler, you can use this parameter to specify the reason for stopping the task here, and the message appears in subsequent DescribeTasks> API operations on this task.

Raises:

  • AccessDeniedException –

    You don't have authorization to perform the requested action.

  • ClientException –

    These errors are usually caused by a client action. This client action might be using an action or resource on behalf of a user that doesn't have permissions to use the action or resource. Or, it might be specifying an identifier that isn't valid.

  • ClusterNotFoundException –

    The specified cluster wasn't found. You can view your available clusters with ListClusters. Amazon ECS clusters are Region specific.

  • InvalidParameterException –

    The specified parameter isn't valid. Review the available parameters for the API request.

    For more information about service event errors, see Amazon ECS service event messages.

  • ServerException –

    These errors are usually caused by a server issue.

  • UnknownServiceError –

    The service returned an error code this client does not model.

Examples:

To stop a task This example stops a task with ID "1dc5c17a-422b-4dc4-b493-371970c6c4d6" in cluster "MyCluster".

>>> client.stop_task(cluster='MyCluster', task='1dc5c17a-422b-4dc4-b493-371970c6c4d6', reason='testing stop task.')
Source code in docs_src/capo_ecs/_services/ecs.py
def stop_task(
    self,
    task: "capo_ecs.types.string.String",
    *,
    config_overrides: Optional[ECSClientConfig] = None,
    cluster: Optional["capo_ecs.types.string.String"] = None,
    reason: Optional["capo_ecs.types.string.String"] = None,
) -> "capo_ecs.types.stop_task_response.StopTaskResponse":
    """<p>Stops a running task. Any tags associated with the task will be deleted.</p> <p>When you call <code>StopTask</code> on a task, the equivalent of <code>docker stop</code> is issued to the containers running in the task. This results in a stop signal value and a default 30-second timeout, after which the <code>SIGKILL</code> value is sent and the containers are forcibly stopped. This signal can be defined in your container image with the <code>STOPSIGNAL</code> instruction and will default to <code>SIGTERM</code>. If the container handles the <code>SIGTERM</code> value gracefully and exits within 30 seconds from receiving it, no <code>SIGKILL</code> value is sent.</p> <p>For Windows containers, POSIX signals do not work and runtime stops the container by sending a <code>CTRL_SHUTDOWN_EVENT</code>. For more information, see <a href="https://github.com/moby/moby/issues/25982">Unable to react to graceful shutdown of (Windows) container #25982</a> on GitHub.</p> <note> <p>The default 30-second timeout can be configured on the Amazon ECS container agent with the <code>ECS_CONTAINER_STOP_TIMEOUT</code> variable. For more information, see <a href="https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-agent-config.html">Amazon ECS Container Agent Configuration</a> in the <i>Amazon Elastic Container Service Developer Guide</i>.</p> </note>

    Args:
        cluster: <p>The short name or full Amazon Resource Name (ARN) of the cluster that hosts the task to stop. If you do not specify a cluster, the default cluster is assumed.</p>
        task: <p>Thefull Amazon Resource Name (ARN) of the task.</p>
        reason: <p>An optional message specified when a task is stopped. For example, if you're using a custom scheduler, you can use this parameter to specify the reason for stopping the task here, and the message appears in subsequent <a href="https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_DescribeTasks.html">DescribeTasks</a>&gt; API operations on this task.</p>

    Raises:
        capo_ecs.errors.access_denied_exception.AccessDeniedException: <p>You don't have authorization to perform the requested action.</p>
        capo_ecs.errors.client_exception.ClientException: <p>These errors are usually caused by a client action. This client action might be using an action or resource on behalf of a user that doesn't have permissions to use the action or resource. Or, it might be specifying an identifier that isn't valid.</p>
        capo_ecs.errors.cluster_not_found_exception.ClusterNotFoundException: <p>The specified cluster wasn't found. You can view your available clusters with <a href="https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_ListClusters.html">ListClusters</a>. Amazon ECS clusters are Region specific.</p>
        capo_ecs.errors.invalid_parameter_exception.InvalidParameterException: <p>The specified parameter isn't valid. Review the available parameters for the API request.</p> <p>For more information about service event errors, see <a href="https://docs.aws.amazon.com/AmazonECS/latest/developerguide/service-event-messages-list.html">Amazon ECS service event messages</a>. </p>
        capo_ecs.errors.server_exception.ServerException: <p>These errors are usually caused by a server issue.</p>
        capo_ecs.errors.UnknownServiceError: The service returned an error code this client does not model.

    Examples:
        To stop a task
        This example stops a task with ID "1dc5c17a-422b-4dc4-b493-371970c6c4d6" in cluster "MyCluster".

        >>> client.stop_task(cluster='MyCluster', task='1dc5c17a-422b-4dc4-b493-371970c6c4d6', reason='testing stop task.')
    """

    def _handler(
        req: "OperationRequest[capo_ecs.types.stop_task_request.StopTaskRequest]",
    ) -> OperationResponse["capo_ecs.types.stop_task_response.StopTaskResponse"]:
        import capo_ecs._operations.amazon_ec2_container_service_v20141113.stop_task

        output, http_response = (
            capo_ecs._operations.amazon_ec2_container_service_v20141113.stop_task.stop_task(
                req.options, req.input
            )
        )
        return OperationResponse(output=output, response=http_response)

    def _redact_output(
        output: "capo_ecs.types.stop_task_response.StopTaskResponse",
    ) -> dict[str, Any]:
        import capo_ecs.types.stop_task_response

        return capo_ecs.types.stop_task_response.redact_sensitive(output)

    interceptors_, options_ = self.operation_options(
        config_overrides, OperationLogging("StopTask", redact_output=_redact_output)
    )
    input_: capo_ecs.types.stop_task_request.StopTaskRequest = {"task": task}
    if cluster is not None:
        input_["cluster"] = cluster
    if reason is not None:
        input_["reason"] = reason

    response = execute_pipeline(
        OperationRequest(input=input_, options=options_),
        handler=_handler,
        interceptors=list(interceptors_),
    )
    response.response.close()
    return response.output