Mastering Application Health via Exit Codes
Measuring application health based on application exit codes
Mastering Application Health via Exit Codes
Measuring application health based on exit codes is a fundamental skill for DevOps engineers. In the context of AWS, these numeric values (0–255) are the primary mechanism by which services like AWS CodeBuild, AWS CodeDeploy, and Amazon ECS determine if a process succeeded or failed.
Learning Objectives
- Define standard POSIX exit codes and their meanings.
- Implement logic in CI/CD pipelines to catch and respond to non-zero exit codes.
- Configure AWS CodeBuild and CodeDeploy to interpret application health through script return values.
- Distinguish between application-level failures and system-level signals (e.g., SIGKILL vs. SIGTERM).
Key Terms & Glossary
- Exit Code (Return Code): An integer returned by a child process to its parent process when it finishes execution.
- POSIX: A set of formal standards that define the API for software compatible with Unix/Linux systems, including exit code conventions.
- $?: A shell variable in Linux/Unix that stores the exit status of the last executed command.
- SIGKILL (137): An exit code indicating the process was forcefully terminated (often due to Out of Memory/OOM conditions).
- Idempotency: The property where an operation can be applied multiple times without changing the result beyond the initial application, critical for exit code retry logic.
The "Big Idea"
In an automated ecosystem like AWS, the infrastructure cannot "see" your code's internal logic. It relies on the Exit Code Interface. By standardizing how your application exits, you enable AWS services to automatically roll back failed deployments, trigger CloudWatch alarms, or halt a pipeline before bad code reaches production.
Formula / Concept Box
| Exit Code | Typical Meaning | AWS Service Action |
|---|---|---|
| 0 | Success | Continue to next step/Success state |
| 1 | General Error | Stop pipeline / Mark Build as FAILED |
| 126 | Command cannot execute | Check permissions on the script/binary |
| 127 | Command not found | Check $PATH or installation step |
| 130 | Terminated by Ctrl+C | Manual intervention detected |
| 137 | Fatal error (Signal 9) | Often indicates Out of Memory (OOM) in ECS |
Hierarchical Outline
- I. Anatomy of an Exit Code
- Standard Range: 0 to 255.
- Reserved Codes: 1, 2, 126, 127, 128+ are reserved by the shell.
- II. Integration with AWS CodeBuild
buildspec.ymlexecution: Each command in thecommandssection must return 0 for the phase to succeed.finallyblocks: Commands that run regardless of previous exit codes.
- III. Health Checks in AWS CodeDeploy
- Lifecycle Event Hooks: Scripts (e.g.,
ValidateService) must exit with 0 to proceed to the next lifecycle event.
- Lifecycle Event Hooks: Scripts (e.g.,
- IV. Container Health (ECS/EKS)
- Task State: ECS monitors the exit code of the primary container to determine if the Task should be restarted.
Visual Anchors
Application Execution Flow
Mapping Exit Code Categories
Definition-Example Pairs
- Term: Exit Code 127
- Definition: The shell was unable to find the command specified in the script.
- Example: In a CodeBuild
buildspec.yml, if you typenpm runn build(typo) instead ofnpm run build, the shell returns 127, and AWS CodeBuild marks the phase as failed immediately.
Worked Examples
Example 1: Custom Health Check Script
Suppose you need to verify if a database is reachable before allowing a CodeDeploy deployment to finish. You write a script check_db.sh:
#!/bin/bash
# Attempt to ping the DB
python3 check_connection.py
STATUS=$?
if [ $STATUS -eq 0 ]; then
echo "Database is healthy."
exit 0
else
echo "Database unreachable! Code: $STATUS"
exit 1
fi[!TIP] Always capture the exit status immediately using
STATUS=$?because subsequent commands (likeecho) will overwrite the$?variable.
Checkpoint Questions
- What exit code is returned if a process is killed by the system due to an Out of Memory (OOM) error?
- In a shell script, which special variable holds the exit code of the last executed command?
- True or False: If an AWS CodeDeploy
AfterInstallscript exits with code 1, the deployment will still be marked as successful.
▶Click to expand answers
- 137 (128 + Signal 9).
- $?
- False. Any non-zero exit code in a CodeDeploy hook causes the deployment to fail.
Muddy Points & Cross-Refs
- Exit Code 137 vs 143: Both indicate external termination. 137 is a
SIGKILL(immediate, no cleanup), while 143 isSIGTERM(graceful request to stop). Understanding this helps diagnose if your ECS task was stopped by the user (143) or crashed by the kernel (137). - Cross-Ref: For more on how these codes trigger alerts, see the CloudWatch Alarms study guide.
Comparison Tables
Shell Built-in Exit Codes
| Code | Reason | Fix |
|---|---|---|
| 126 | Permission Denied | Run chmod +x script.sh |
| 127 | Command Not Found | Check your PATH or installation path |
| 128+n | Fatal Error Signal "n" | Identify signal (e.g., n=9 is SIGKILL) |