Two kinds of exit#
A new revision rolls out and its tasks stop. Every stopped task has containers with an exitCode, so the code alone does not say who ended the process. The task's stopCode does. Task fields · The signature table
- The process exited on its own. If a container marked
essentialstops for any reason, ECS stops the other containers in the task, and the task's stop code isEssentialContainerExited. The 2023 developer guide gives the stopped reason asEssential container in task exited. Here the exit code is the evidence about the release. ContainerDefinition · Stopped task reasons - ECS stopped it. For a stop that ECS starts, such as a health check replacement, a scale-in, a Spot interruption or a person's
StopTaskcall, the task moves toSTOPPING. The agent sendsSIGTERMto Linux containers andSIGKILLonce the container's stop timeout passes. The exit code then records how the process took the shutdown, not why the task stopped, and the stop code names the cause. Task lifecycle · StopTask
The stop code guide covers the other codes. This guide reads the exit codes under EssentialContainerExited.
Read the stop code, the exit codes and the timing#
Use a read-only profile. List the service's stopped tasks, then read each task's stop code, timestamps and every container's exit code and reason. Stopped tasks remain visible for at least an hour. describe-tasks · Stopped task retention
aws ecs list-tasks \
--profile PROFILE --region REGION --cluster CLUSTER \
--service-name SERVICE --desired-status STOPPED \
--query 'taskArns' --output json --no-cli-pager
aws ecs describe-tasks \
--profile PROFILE --region REGION --cluster CLUSTER --tasks TASK_ARN_1 TASK_ARN_2 \
--query 'tasks[].{deployment:startedBy,definition:taskDefinitionArn,started:startedAt,stopping:stoppingAt,stopped:stoppedAt,code:stopCode,reason:stoppedReason,containers:containers[].{name:name,exit:exitCode,reason:reason,image:imageDigest}}' \
--output json --no-cli-pager
startedAt is when the task entered RUNNING, and stoppingAt is when it moved from RUNNING to STOPPING. Under EssentialContainerExited, the gap between them is roughly how long the process ran. A gap of seconds points at startup: configuration the new revision reads, a missing file or variable, a migration or connection at boot. A gap of minutes or hours points at something the process met while serving. Task fields
In a task with several containers, read each one. The essential container that ended first carries the code that matters. The others were stopped by ECS afterwards, so their codes reflect the shutdown, usually 0 or 143.
What each exit code usually means#
Docker reports the contained command's own exit status, except for a few codes it reserves for failures to run the command. A process ended by a signal reports 128 plus the signal number, the convention the Bash manual documents. Signal numbers below are the Linux values. Docker exit status · Bash exit status · signal(7)
| Exit code | What it records | Where to look next |
|---|---|---|
| 0 | The process reported success and ended | A service's main process returned: the command ran a one-off step, or the real process moved to the background and the foreground exited |
| 1 | The program's own general failure status | The container's last log lines before stoppingAt |
| 126 | A command was found but could not be run | File permissions or the executable bit on the entry point or a script it calls |
| 127 | A command was not found | A binary the entry point or a script calls is missing from the image or from PATH |
| 139 | 128 + 11: killed by SIGSEGV, an invalid memory reference | Native code: the base image, runtime version, native libraries or CPU architecture the revision changed |
| 143 | 128 + 15: killed by SIGTERM | Something sent a termination signal; under EssentialContainerExited, find what inside the task sent it |
| 255 | The low eight bits of the status were all ones; a program that calls exit(-1) reports 255 | The container's logs; the code itself says only that the program failed |
Only the low eight bits of a status reach the parent process, so an out-of-range value wraps: -1 becomes 255, and a status of 256 would read as 0. Exit code 137, a SIGKILL, has its own guide. exit(3p)
If the image lacks the entry point itself, the container usually never runs. That task has no startedAt and belongs with the startup failures in the signature table, not here.
Read the logs for the exit window#
With the awslogs driver and a stream prefix, each container writes to a stream named prefix-name/container-name/ecs-task-id. Look up the log group and prefix in the failed revision, then read the stream for the task ID. LogConfiguration
aws ecs describe-task-definition \
--profile PROFILE --region REGION --task-definition TASK_DEFINITION_ARN \
--query 'taskDefinition.containerDefinitions[].{name:name,essential:essential,entryPoint:entryPoint,command:command,stopTimeout:stopTimeout,restart:restartPolicy,logDriver:logConfiguration.logDriver,logOptions:logConfiguration.options}' \
--output json --no-cli-pager
fields @timestamp, @logStream, @message
| filter @logStream like /TASK_ID/
| sort @timestamp desc
| limit 50
Compare the last log line's time with stoppingAt. If the application logged a fatal error and then the task moved to STOPPING, the process ended on its own. If stoppingAt comes first and the shutdown messages follow it, ECS asked the process to stop, and the stop code names why.
An illustrative rollout, two outcomes#
This fixture is fictional. The service checkout rolls out checkout:42 in deployment ecs-svc/2211. Two readings of its stopped tasks:
| Outcome | Stop code | startedAt to stoppingAt | Containers |
|---|---|---|---|
| A | EssentialContainerExited | 4 seconds | app exit 127; log-router exit 0 |
| B | ServiceSchedulerInitiated | 6 minutes | app exit 143 |
In outcome A, app ended four seconds after start with 127, and the log router stopped after it. Its logs show the entry point script calling a tool that the new, smaller base image does not include. The release is the cause, and the change is to the image or the script.
In outcome B, the 143 records how app died, and the stop code says the scheduler stopped the task. The stopped reason names a failed health check. The exit code here shows only that app does not handle SIGTERM; the health check is the question, which the container health check guide covers.
What to change, and how to confirm#
Fix what the exit code and logs point at in the revision, then deploy it as a new revision. Two settings change how exits behave, and each is a change to the task definition:
- Container restart policy. With
restartPolicyenabled, ECS can restart a container that exits without replacing the task. Exit codes listed inignoredExitCodesare not restarted, and a container must run forrestartAttemptPeriod, 300 seconds by default, before a restart is attempted. A container that crashes at startup therefore still stops the task. ContainerRestartPolicy · Restart policies - Signal handling and
stopTimeout. A process that handlesSIGTERMcan finish its work and exit 0 within the stop timeout, 30 seconds by default and at most 120 on Fargate. The image'sSTOPSIGNALcan change which signal is sent. StopTask · ContainerDefinition
On the replacement, confirm that the new deployment's tasks stay RUNNING beyond the window in which the old ones exited, and that no stopped task from that deployment carries EssentialContainerExited.
Where the reading stops#
An exit code is eight bits. It cannot say why a program chose 1 or 255, and a program can pick any code, including ones that look like signals, so read the logs before trusting a 128-plus value. Without a log driver, or for output written somewhere other than standard output and error, the logs are not in CloudWatch. Stopped tasks expire, so keep task state change events through EventBridge if you need the records later. Task state change events
Questions#
What does exit code 143 mean on an ECS task?
The process was killed by SIGTERM, signal 15, which reports as 128 + 15. If the stop code is anything but EssentialContainerExited, ECS sent the signal while stopping the task, and the stop code says why.
Why does my ECS container exit with code 0 and the task still stop?
An essential container that exits stops the task, whatever its code. Exit code 0 means the main process ended normally, which for a service usually means the start command ran something that finishes, or handed off to a background process.
How can I tell whether ECS stopped my task or the application crashed?
Read stopCode. EssentialContainerExited means a container ended on its own; codes such as ServiceSchedulerInitiated, UserInitiated or SpotInterruption mean ECS stopped the task, and the exit codes describe the shutdown.