Exit Code 137 vs. Exit Code 143: Diagnosing Kernel OOM Killer (SIGKILL) vs. Supervisor SIGTERM

Last Verified On: Kernel 6.8 / 6.12 LTS / Ubuntu 24.04 LTS & Debian 12/13 Moderate (Service Restart) Exit Code: 137 / 143 (SIGKILL, SIGTERM)

Direct Answer / TL;DR

137 = 128+9 (SIGKILL): on Ubuntu 24.04, Debian 12, and RHEL 9 this means the kernel OOM killer hit a cgroup memory.max; confirm with dmesg/journalctl and raise limits (systemd MemoryMax, docker -m, K8s memory). 143 = 128+15 (SIGTERM): a supervisor stopped the process; fix the TERM handler or extend grace (docker stop -t, TimeoutStopSec, terminationGracePeriodSeconds).

dmesg -T | grep -iE 'out of memory|killed process'

Symptom & Exact Error Signature

Both codes appear as an abrupt death of a process, systemd unit, Docker container, or Kubernetes pod with no application-level error: the reporter (bash, systemd, dockerd, kubelet) records code=killed and the exit status equals exactly 128 + signal number. 137 = 128+9 (SIGKILL) is the OOM-killer signature; 143 = 128+15 (SIGTERM) is a deliberate stop signal from docker stop, kubectl delete pod, systemctl stop/restart, or a manual kill.

$ systemctl status mysql.service | grep 'Main PID'
   Main PID: 1234 (code=killed, status=9/KILL)

$ dmesg -T | grep -i 'killed process'
[Wed Jun 11 03:41:07 2025] Out of memory: Killed process 8123 (mysqld) total-vm:3145728kB, anon-rss:1048576kB, shmem-rss:0kB, UID:102 pgtables:6420kB oom_score_adj:0

$ docker inspect --format '{{.Name}} {{.State.ExitCode}} OOMKilled={{.State.OOMKilled}}' $(docker ps -aq)
/web-1 137 OOMKilled=true
/sidecar 143 OOMKilled=false

$ kubectl -n prod describe pod web-77b6c | grep -A3 'Last State:'
    Last State:          Terminated (Reason: OOMKilled, Exit Code: 137)

Root Cause Breakdown

The 128+n convention. bash, dash, systemd, dockerd, and kubelet all report a signal death as 128 + signal (bash(1) Exit Status): SIGKILL=9 produces 137, SIGTERM=15 produces 143. This is a userland reporting convention; the kernel itself writes no exit codes.

sh -c 'kill -9 $$';    echo $?  # 137 = 128 + 9  (SIGKILL, default disposition)
sh -c 'kill -TERM $$'; echo $?  # 143 = 128 + 15 (SIGTERM, default disposition)

Who sends SIGKILL on exit 137 — SIGKILL cannot be caught, blocked, or ignored, so the sender is unambiguous:

  • Kernel OOM killer (dominant case): global OOM when MemAvailable and swap are exhausted, or memcg-local OOM when a cgroup v2 ceiling is hit — memory.max, i.e. systemd MemoryMax=, Docker -m, Kubernetes resources.limits.memory. The kernel logs Out of memory: Killed process ... (global) or Memory cgroup out of memory: Killed process ... (memcg-local) and increments memory.events:oom_kill. Victim selection uses oom_score; oom_score_adj (-1000..1000) biases it.
  • Manual/scripted kill -9 or pkill -9.
  • Supervisor escalation: docker stop escalates TERM → KILL after its grace (default 10 s); systemd TimeoutStopSec= (default 90 s) then SIGKILL per KillMode=control-group; kubelet forces SIGKILL after terminationGracePeriodSeconds (default 30 s).

Who sends SIGTERM on exit 143. SIGTERM is catchable, so 143 proves the entry process held the default disposition at death time — it did not trap TERM. Typical senders: systemctl stop, docker stop, kubectl delete, kill <pid>. A graceful application that installs a TERM handler and exits cleanly produces 0, never 143. Therefore 143 is best read as stopped at signal level: a real shutdown request that the process did not service — missing trap, entrypoint not being PID 1 (containers), or a grace period too short for the cleanup path.

Step-by-Step Diagnostic & Fix Sequence

  1. Verify the state: identify the exit code and locate the killer.
    # systemd: last main process death
    systemctl show -p ExecMainCode -p ExecMainStatus mysql.service
    # ExecMainCode=killed, ExecMainStatus=9  -> 137; hunt OOM evidence next
    # ExecMainStatus=15                     -> 143; TERM path
    
    # Kernel OOM evidence (this boot, precise timestamps, case-insensitive)
    journalctl -k -o short-precise --grep 'oom|killed process'
    # 137: 'Out of memory: Killed process ...' (global)
    #      'Memory cgroup out of memory: Killed process ...' (memcg limit)
    # 143: no OOM lines -> supervisor-initiated stop
    
    # cgroup v2 kill counters for the exact unit
    systemctl show -p ControlGroup --value mysql.service
    cat /sys/fs/cgroup/system.slice/mysql.service/memory.events
    # oom 3
    # oom_kill 3      <- increments on every memcg OOM kill
    
    # Containers: the authoritative flag (true means memcg OOM inside the container)
    docker inspect --format '{{.Name}} {{.State.ExitCode}} {{.State.OOMKilled}}' $(docker ps -aq)

    Interpretation: OOMKilled=true &#8594; the container breached its own -m/K8s limit; ExitCode=137 with OOMKilled=false plus a kernel OOM line &#8594; host-global OOM or manual kill -9; 143 with no OOM lines &#8594; a stop request from systemd/docker/kubelet (possibly a stop-timeout escalation that happened after TERM).

  2. Apply the fix (a) for OOM/137, (b) for SIGTERM/143:
    # (a) OOM: raise the ceiling for the unit (persistent, applies immediately)
    sudo systemctl set-property mysql.service MemoryHigh=5G MemoryMax=6G
    
    # (a) Shield critical daemons from victim selection
    # Ubuntu/Debian: ssh.socket-activated unit is ssh.service; RHEL 9: sshd.service
    sudo systemctl edit ssh.service            # [Service] OOMScoreAdjust=-1000
    
    # (a) Docker: verify current usage first, then raise (never set below RSS)
    docker stats --no-stream web-1
    docker update -m 2g web-1
    
    # (a) Kubernetes: back up, then edit limits (no blind patching)
    kubectl -n prod get deploy web -o yaml > web-deploy.yaml
    kubectl -n prod edit deployment web        # resources.limits.memory = 1Gi
    
    # (a) Transient, per-PID OOM bias (reverts on restart)
    echo 250 | sudo tee /proc/8123/oom_score_adj
    
    # (b) SIGTERM: give the real shutdown path enough time
    docker stop -t 60 web-1
    sudo systemctl edit mysql.service          # [Service] TimeoutStopSec=90
    kubectl -n prod edit deployment web               # spec.terminationGracePeriodSeconds: 60
    
    # (b) In containers PID 1 must forward/act on TERM (plain PID 1 swallows signals)
    docker run --init --name web-1 webapp:latest
    
    # (b) Shell entrypoints: trap TERM, relay to child, exit 0
    child=$!; trap 'kill -TERM $child; wait $child; exit 0' TERM INT
  3. Confirm resolution:
    # Stop path now ends cleanly (not 143, not 137)
    docker stop -t 60 web-1
    docker inspect -f 'exit={{.State.ExitCode}} killed={{.State.OOMKilled}}' web-1
    # Expected: exit=0 killed=false
    
    # systemd restarts clean
    systemctl restart mysql.service && systemctl is-active mysql.service
    # Expected: active   ($? = 0)
    
    # Counters stay flat across a full workload run
    cat /sys/fs/cgroup/system.slice/mysql.service/memory.events   # oom_kill unchanged
    cat /sys/fs/cgroup/system.slice/mysql.service/memory.peak     # peak < memory.max
    
    # Kubernetes: no fresh OOMKilled terminations
    kubectl -n prod get events --field-selector reason=OOMKilled --sort-by=.lastTimestamp | wc -l
    # Expected: 0

Multi-Distro Command & Path Matrix

Ubuntu / Debian

# Logs: journald persistent (Ubuntu) + rsyslog kern.log (Debian 12)
journalctl -k -o short-precise --grep 'oom|killed process'
grep -iE 'out of memory|oom' /var/log/kern.log

# userspace early-kill daemon (Ubuntu 24.04 active; Debian 12 usually inactive)
systemctl is-active systemd-oomd; oomctl
journalctl -u systemd-oomd --since -7d | grep -i killed

# cgroup v2 counters (Ubuntu 24.04 / Debian 12 default v2)
cat /sys/fs/cgroup/system.slice/ssh.service/memory.events

# Fix: raise unit cap; Ubuntu ssh unit is ssh.service, not sshd.service
sudo systemctl set-property mysql.service MemoryHigh=5G MemoryMax=6G

RHEL / AlmaLinux / Rocky

journalctl -k --grep 'oom|killed process'
grep -iE 'oom|killed process' /var/log/messages   # rsyslog default
cat /sys/fs/cgroup/system.slice/httpd.service/memory.events

# Raise cap (cgroup v2 is default on RHEL 9)
sudo systemctl set-property httpd.service MemoryHigh=4G MemoryMax=5G

# Ad-hoc memory sandbox for risky jobs
sudo systemd-run --scope -p MemoryMax=2G /opt/batch/run.sh

# Sanity: OOM must not panic the box
sysctl vm.panic_on_oom                           # must be 0

Arch Linux

journalctl -k --grep 'oom|killed process'
cat /sys/fs/cgroup/system.slice/docker.service/memory.events

# Choose an early-kill daemon (both ship in repos)
systemctl enable --now systemd-oomd              # or: pacman -S earlyoom && systemctl enable --now earlyoom

# Per-unit cap via drop-in; revert with systemctl revert <unit>.service
sudo systemctl edit <unit>.service

Alpine / Containers

# BusyBox dmesg has no -T; no systemd --grep here
dmesg | grep -iE 'oom|killed process'
grep -i oom /var/log/messages                    # busybox syslogd

# cgroup counters: v2 = /sys/fs/cgroup/memory.events
#                 v1 = /sys/fs/cgroup/memory/memory.oom_fail_cnt + per-memcg memory/.../memory.events

docker inspect --format '{{.Name}} {{.State.ExitCode}} {{.State.OOMKilled}}' $(docker ps -aq)
docker update -m 2g <ctr>

# OpenRC service lifecycle
rc-service docker restart; rc-status -a

Safety & Rollback Checks

Safety Check: Take read-only snapshots before any limit change: systemctl show -p MemoryMax -p MemoryHigh -p OOMScoreAdjust <unit>.service, free -h, docker stats --no-stream <ctr>, and kubectl diff -f web-deploy.yaml (or kubectl apply --dry-run=server -f) as the dry-run for kube changes.

Rollback: systemd — sudo systemctl revert <unit>.service deletes the generated drop-ins (no manual rm needed); Docker — record first with docker inspect -f '{{.HostConfig.Memory}}' web-1, then restore with docker update -m <old>; Kubernetes — kubectl rollout undo deployment/web.

Hazards: Setting -m/MemoryMax= below the process’s current RSS kills it instantly; writing oom_score_adj via echo > /proc/<pid>/oom_score_adj is per-PID and lost on restart; do not kill -9 as a diagnostic; keep vm.panic_on_oom=0; if free -h shows negligible MemAvailable, fix the leak or add swap before raising limits, or you only move the kill elsewhere.

Frequently Asked Edge Cases

docker inspect shows OOMKilled=false but my container still exits 137 — who killed it?

SIGKILL came from outside the container's memory limit: either the host-global OOM killer or a manual/scripted kill -9. Correlate timestamps with journalctl -k --grep 'killed process' and inspect host memory pressure with free -h; also rule out docker stop grace expiry, which escalates to SIGKILL and can report 137.

My application traps SIGTERM, yet systemd still reports the unit as code=killed status=15/TERM (143). Why?

status=15/TERM means the main PID died at the signal level, so the trap never ran or the process ignored TERM. In containers, a non-forwarding PID 1 is the usual culprit — run with docker run --init or set STOPSIGNAL and forward signals in the entrypoint; on hosts, verify the trap with sh -c 'trap "echo got TERM" TERM; sleep 60' and check TimeoutStopSec is not killing mid-cleanup.

How can I reproduce exit codes 137 and 143 safely for testing?

Signal mapping: sh -c 'kill -9 $$'; echo $? prints 137, and sh -c 'kill -TERM $$'; echo $? prints 143. Forced memcg OOM: docker run --rm -m 64m busybox tail /dev/zero exits 137 with .State.OOMKilled=true; a sandboxed host test is systemd-run --scope -p MemoryMax=64M stress-ng --vm 1 --vm-bytes 128m --timeout 60s, which ends with a Killed process line in journalctl -k.

References & Upstream Docs

  1. Linux man-pages: man 1 bash (Exit Status: 128+n), man 5 proc (oom_score_adj), man 7 signal
  2. systemd documentation: man systemd.resource-control (MemoryMax, MemoryLow), man systemd.kill (TimeoutStopSec, KillMode)
  3. Kernel documentation: Documentation/admin-guide/cgroup-v2.rst (memory controller, memory.events)
  4. Docker docs (docker update, --memory, --init) and Kubernetes docs (Resource Management for Pods, terminationGracePeriodSeconds)

Related Concepts