Learning objectives
After reading this article you will be able to:
- Read an exit code and tell whether it came from the app or from a signal
- Find the cause of 137, 143, 139, 127 and 1
- Fix the problem and make your app shut down cleanly
On this page
- What does the error look like?
- How do you read an exit code?
- What does each code mean?
- Exit code 137: memory or a forced kill?
- Exit codes 143 and 130: who asked it to stop?
- Exit codes 1, 126, 127 and 139: the app or the environment?
- How do you find the cause step by step?
- How to do this on Square Cloud
- FAQs
The process was running, and then it stopped. In the logs, in docker ps or in kubectl, all that's left is a number: 137, 143, 1. That number is the exit code, and it tells you whether the process finished on its own, whether something asked it to stop or whether it was killed without warning.
What does the error look like?#
In Docker, the code appears in the STATUS column of docker ps -a:
$ docker ps -a --filter "exited=137"
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3f2a9c1d7b42 api "node index.js" 2 hours ago Exited (137) 5 seconds ago apiIn Kubernetes, in the container's previous state (kubectl get pod <name> --output=yaml):
lastState:
terminated:
exitCode: 137
reason: OOMKilledAnd in a terminal, Bash tells you the process was killed and stores the code in $?:
$ node index.js
Killed
$ echo $?
137How do you read an exit code?#
An exit code is a number from 0 to 255 that a process returns when it ends. 0 means success; any other value means failure. The rest follows conventions:
- 1 to 125: the program chose the code itself. The Unix convention, as the Python documentation notes, is 2 for command line syntax errors and 1 for all other errors.
- 126 and 127: these come from the shell. 127 means command not found; 126 means the command was found but is not executable.
- Above 128: the process was ended by a signal. The Bash manual defines the value as 128 + N, where N is the signal number, and Node.js follows the same rule.
To decode a high code, subtract 128 and look up the signal. Bash does the math for you:
kill -l 137 # KILL
kill -l 143 # TERM
kill -l 139 # SEGVThe value is only 8 bits wide: on Linux, the parent process receives only the lowest byte (status & 0xFF), and the POSIX specification guarantees the same for wait() and waitpid(). That is why exit 256 arrives as 0 and exit 300 arrives as 44.
What does each code mean?#
| Code | Meaning | Most common cause | First step |
|---|---|---|---|
| 0 | Success | The program reached its end | If it should stay up, find what stopped holding the process open |
| 1 | Generic error | Uncaught exception | Read the stack trace at the end of the log |
| 2 | Incorrect usage | Invalid option or argument | Review the start command |
| 126 | Not executable | Script without execute permission | chmod +x or call it through the interpreter |
| 127 | Command not found | Typo or missing program | Check the command and the PATH |
| 130 | SIGINT (128 + 2) | Ctrl+C in a terminal | Normal when you stop it by hand |
| 134 | SIGABRT (128 + 6) | The process called abort() on a fatal error |
Read the lines right before the stop |
| 137 | SIGKILL (128 + 9) | Memory limit or forced kill | Check the memory graph |
| 139 | SIGSEGV (128 + 11) | Invalid memory access in native code | Reinstall or update native modules |
| 143 | SIGTERM (128 + 15) | Stop request (deploy, restart, docker stop) |
Handle SIGTERM and exit with 0 |
Exit code 137: memory or a forced kill?#
137 only says the process received SIGKILL, the signal that, per the signal(7) man page, cannot be caught, blocked or ignored. The process runs no cleanup code, so the log stops mid-stream with no error. The causes, from most to least common:
- Going over the memory limit. In a container, when usage hits the limit and the kernel can't reclaim memory, it kills processes inside the container. This is the OOM kill.
- SIGTERM with no effect.
docker stopsends SIGTERM and, if the container hasn't exited after 10 seconds (the Linux default), sends SIGKILL. In Kubernetes, the default grace period is 30 seconds. The process ends with 137 instead of 143 when it ignores SIGTERM, takes too long to close, or is the container's PID 1 with no handler (per the pid_namespaces(7) man page, Linux only delivers to PID 1 the signals it handles, SIGKILL aside). - A manual kill or another program. A
kill -9, a process supervisor or a maintenance script.
To tell the first cause from the others, ask the tool whether there was an OOM:
docker inspect --format '{{.State.ExitCode}} {{.State.OOMKilled}}' api
# 137 true -> ran out of memory
# 137 false -> SIGKILL for another reason
kubectl get pod api --output=yaml # look for "reason: OOMKilled"Outside Docker and Kubernetes, the memory graph answers it: a line that climbs to the limit and drops to zero at the moment of the stop is an OOM. Diagnosing the leak, with Node.js, Python and Java examples, is covered in out of memory (OOM) errors. To pick a limit with headroom, see how much RAM and CPU your app needs.
Exit codes 143 and 130: who asked it to stop?#
143 is SIGTERM, a polite request to stop, which docker stop, Kubernetes and many platforms send before replacing an instance during a deploy, a restart or a scale-down. Right after a deploy, it is usually normal. 130 is SIGINT, the same signal as Ctrl+C in a terminal.
The trouble is dying in the middle of a request or a job. The Twelve-Factor methodology describes a clean shutdown: on SIGTERM, the process stops accepting connections, finishes the work in progress and exits. In Node.js, SIGTERM has a default handler that exits with 128 + 15; once you register a listener, that default goes away and the decision is yours:
import http from "node:http";
const server = http.createServer((req, res) => res.end("ok"));
server.listen(Number(process.env.PORT ?? 3000), "0.0.0.0");
process.on("SIGTERM", () => {
console.log("SIGTERM received, closing the server");
server.close(() => process.exit(0));
// if a connection holds close() open, exit before SIGKILL arrives
setTimeout(() => process.exit(1), 8_000).unref();
});By default, Python installs handlers only for SIGINT and SIGPIPE, so SIGTERM ends the process without running finally blocks. Register a handler with the signal module:
import signal
import sys
import time
def shutdown(signum, frame):
print("SIGTERM received, shutting down", flush=True)
sys.exit(0) # raises SystemExit: finally blocks run
signal.signal(signal.SIGTERM, shutdown)
while True:
time.sleep(1) # the app's workWith the handler, the process closes its database or bot Gateway connections and exits with 0. That work has to fit inside the grace period before SIGKILL.
Exit codes 1, 126, 127 and 139: the app or the environment?#
Exit code 1 is the most common and the least specific. In Node.js, it is the code for an uncaught exception, and by default a Promise rejected without a catch becomes an uncaught exception too. In Python, an exception that reaches the top prints the traceback and exits with 1. The cause is in the last log lines before the stop: a missing environment variable, a database refusing the connection, a port already in use (EADDRINUSE) or a missing module (Cannot find module).
Exit code 127 almost always points at the start command. The message depends on the shell; in Bash:
bash: line 1: tsx: command not foundBesides a typo or a program that doesn't exist in the environment, the classic cause is a development tool called in production: with NODE_ENV=production, npm install skips devDependencies, so tsx, nodemon or ts-node disappear. Move the tool to dependencies or compile ahead of time, as shown in the best way to run TypeScript in production.
Exit code 126 shows up when the command exists but can't be executed, usually a script without execute permission. Run chmod +x start.sh and record the permission in Git (git update-index --chmod=+x start.sh), or call it through the interpreter: bash start.sh.
Exit code 139 is SIGSEGV, an invalid memory access. Plain JavaScript or Python code rarely causes it; the source is usually native code: a Node.js addon, a C extension in a Python package, a system library. Install dependencies in the environment where the app runs (npm rebuild, or delete node_modules and install again) instead of copying the folder from another machine, and update the suspect package. If the build fails, see npm install failed on deploy. In Python, python -X faulthandler main.py prints the traceback at the moment of the crash.
How do you find the cause step by step?#
- Get the exact code:
echo $?in a terminal,docker inspect,kubectl get pod --output=yamlor your host's status screen. Above 128, decode the signal withkill -l. - Read the last log lines before the stop. A stack trace points to an app error; a log cut off mid-stream points to a signal. See how to read your app's logs.
- Compare with the memory graph and the deploy times. If memory climbed to the limit, it is an OOM; a 143 in the same minute as a deploy is the instance being replaced.
- Reproduce locally with the same limit:
docker run --rm --memory=512m apishows whether the app fits in the memory it has in production.
How do you keep it from happening again?#
- Handle SIGTERM and exit with 0.
- Set the runtime's heap ceiling below the container limit, so the failure is a readable error instead of a silent 137.
- Know what your supervisor restarts. In Docker, the
on-failurepolicy restarts when the code is non-zero. In systemd,Restart=on-failuredoesn't restart after a clean exit, which includes 0 and the signals SIGHUP, SIGINT, SIGTERM and SIGPIPE. A bot that exits with 0 by mistake stays down in both. - In a restart loop, read the first error, not the last. PM2, for example, stops restarting after a run of crashes right after startup, counted by its
max_restartsoption (PM2 and auto-restart).
How to do this on Square Cloud#
On Square Cloud, automatic restart is on by default for dashboard deploys (in a hand-written squarecloud.app, only with AUTORESTART=true). It only acts when the process exits with an error: in that case the application comes back on its own, without PM2, which is neither needed nor recommended.
Running out of memory is the exception: the application stops and does not come back on its own. Increase the application's RAM in its settings or through the API, then start it again. CPU, RAM and network metrics are kept for 24 hours for every application (7 days on Pro and Enterprise, for applications from 512 MB), and the RAM graph shows whether memory climbed to the limit.
Real-time logs are in the dashboard, the CLI, the VS Code extension and the mobile app. Cubic, the AI agent in the dashboard, reads the logs to diagnose failures when you ask, and the check-up (/checkup in the chat) compares the RAM peak of the last 24 hours with the limit and flags automatic restart when it is off.
See the deploy, logging and metrics features in Square Cloud features.
Deploy
Push from GitHub, the CLI or a ZIP and go live in seconds
FAQs
Is exit code 137 always out of memory?
No. 137 only says the process received SIGKILL. In a container, running out of memory is the most common cause, but a manual kill -9 or a process that didn't exit in time after SIGTERM give the same code.
What is the difference between exit code 137 and 143?
143 is SIGTERM (128 + 15): something asked the process to stop, and it can handle the request. 137 is SIGKILL (128 + 9): the process is ended on the spot, with no chance to react.
What does exit code 0 mean?
That the process finished successfully. For a bot or server that should stay up, an unexpected 0 means the code reached its end, for example because nothing kept the process alive.
What does exit code 127 mean?
That the shell could not find the command. It is usually a typo in the start command or a program that isn't installed in the environment, such as a tool that only exists in devDependencies.
Why does an exit code never go above 255?
Because the parent process only receives the lowest byte of the value passed on exit (status & 0xFF). An exit 256 arrives as 0 and an exit 300 arrives as 44.
More in Deploy
See all 38 articles- What are environment variables?Environment variables are key-value pairs the operating system hands to a process at startup. Learn to read them in Node.js and Python and keep tokens out of code.
- What is a deployment?A deployment takes a version of your code to a server, prepares it and starts it. See the stages, the methods and how to roll back when it goes wrong.
- How much RAM and CPU does my app need?Size RAM and CPU by measuring your app's real peak and adding about 30% headroom. Learn how to measure in Node.js, Python and Java and what to do if short.
- IaaS vs PaaS vs SaaS: the differences, with examplesIaaS rents machines, PaaS gives you an environment to run your code and SaaS delivers a finished app. Compare who is responsible for what, with examples.