Skip to main content

πŸ“˜ Exit Status Code = 137 in Bash



🎯 What You Will Learn​

  1. Understand what exit status code = 137 means in Bash.
  2. Learn how it relates to SIGKILL (signal 9) and forced termination.
  3. Identify system-level causes such as Out-of-Memory (OOM) kills or admin intervention.
  4. Distinguish 137 from other signal-based codes like 130 (SIGINT).
  5. Learn how to detect, prevent, and recover from 137 in WordPress VPS automation.
  6. Apply graceful mitigation techniques to keep long-running tasks safe.

1. 5W + 1H Framework​

ElementDescription
WhatExit status 137 indicates that a process was killed by signal 9 (SIGKILL) β€” a forceful termination signal that cannot be trapped or ignored.
WhyIt happens when the kernel, system administrator, or container manager forcibly kills a process β€” commonly due to out-of-memory (OOM) or manual termination (kill -9).
WhoSystem administrators, Bash developers, or VPS owners managing resource-intensive tasks like backups, imports, or WP cron jobs.
WhenWhen the process exceeds available memory, CPU limits, or is manually force-stopped.
WhereCommon in VPS environments, Docker containers, long-running WP-CLI commands, or large cron batch jobs.
HowBash calculates exit code as 128 + signal_number β†’ 128 + 9 = 137.

2. Prerequisites​

You should already:

  • Know the difference between graceful exits (130) and forceful kills (137).
  • Understand Linux signals (SIGINT, SIGTERM, SIGKILL).
  • Know how to check system logs (dmesg, /var/log/syslog) and memory usage.
  • Be comfortable using commands like ps, kill, top, and free.

3. Core Syntax and Examples​

🧩 Definition​

Exit code 137 means:

Process terminated by SIGKILL (signal 9) β†’ 128 + 9 = 137.


Example 1 – Manual Kill Using kill -9​

sleep 60 &
kill -9 $!
wait $!
echo $?

Expected Output:

137

Explanation: You manually sent signal 9 to terminate the background process. SIGKILL is non-catchable β€” the process is stopped instantly. Use Case (WordPress VPS): Manually killing a long-running WP migration or backup script.​

Example 2 – Out-of-Memory (OOM) Killer​

# Simulate memory overload (do not run in production)
python3 -c "a=[' ' * 10**8]*100"
echo $?

Expected Output:

Killed
137

Explanation: The process consumed too much memory. The Linux kernel invoked OOM Killer to protect system stability. Result β†’ exit code 137. Use Case: Large wp export or mysqldump consuming excessive memory on a low-RAM VPS.​

Example 3 – Docker Container Exceeded Memory Limit​

docker run --memory=256m ubuntu bash -c "python3 -c 'a=[0]*10**8'"
echo $?

Expected Output:

Killed
137

Explanation: Docker enforces a memory cap (256m). When the process exceeds it, Docker sends SIGKILL. Container exits with code 137. Use Case: Running WP cron jobs in containerized environments with strict limits.​

Example 4 – Script Terminated Midway​

#!/bin/bash
echo "Starting long task..."
for i in {1..100}; do
echo "Processing $i..."
sleep 1
done

Run:

bash longtask.sh &
kill -9 $!
wait $!
echo $?

Expected Output:

137

Explanation: The script was running normally until forcibly stopped. Exit 137 indicates non-graceful termination.​

Example 5 – Verifying OOM Event from Logs​

dmesg | grep -i "killed process"

Expected Output Example:

[12345.6789] Out of memory: Killed process 2456 (php) total-vm:2048000kB, anon-rss:1024000kB, ...

Explanation: System logs confirm kernel killed a PHP or Bash process due to memory exhaustion.​

4. Common Scenarios Triggering Exit Code 137​

ScenarioExampleDescription
Manual force killkill -9 PIDAdmin or automation forcibly terminates
OOM Killermysqldump large_dbSystem runs out of memory
Docker memory capdocker run --memory=512m ...Container exceeds allowed RAM
System rebootVPS restart mid-processKernel terminates running processes
Resource contentionToo many background jobsKernel selects one process to kill
Large file compressiontar -czf backup.tar.gz /homeMemory spike triggers OOM

5. WordPress VPS Use Cases​

Use CaseExampleExplanation
Database dump killedmysqldump -u root -p wordpress > backup.sqlLarge DB triggers OOM kill
WP import/exportwp import bigfile.xmlProcess exceeds PHP memory limit
WP cron timeoutwp cron event run --due-nowVPS kills task after CPU/RAM spike
Redis flush under loadredis-cli flushallConsumes memory rapidly
Backup task in cron/root/wp-backup.shKilled when multiple backups overlap
Heavy image optimizationfind uploads -name '*.jpg' -exec convert {} \;Memory exhaustion under parallel load

6. Troubleshooting Matrix​

SymptomLikely CauseFix
β€œKilled” message in consoleOOM Killer triggeredAdd swap or increase RAM
Exit code 137 in cron logMemory exhaustionOptimize script or lower concurrency
Process killed instantlykill -9 PID issuedAvoid forceful kills; use kill -15 first
Docker container stopsMemory limit exceededRaise --memory limit or optimize app
Random terminationVPS under heavy loadTune MySQL, PHP-FPM, or caching settings

7. Best Practices​

  1. Avoid forceful termination:
  • Prefer kill -15 (SIGTERM) before kill -9.
  • Use graceful shutdowns with trap in scripts.
  1. Monitor system resources:
top
free -h
vmstat 1

  1. Check system logs regularly:
dmesg | grep -i killed

  1. Add swap memory for low-RAM VPS:
fallocate -l 1G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile

  1. Optimize WP-CLI commands:
  • Split large imports.
  • Use -skip-themes and -skip-plugins to reduce load.
  • Run tasks off-peak or with background scheduling.
  1. Container optimization:
  • Adjust Docker memory limits.
  • Add -oom-score-adj to lower kill priority of critical services.

8. Quick Lab – Simulating Exit 137​

Step 1: Run a long process​

sleep 60 &
pid=$!

Step 2: Kill it forcefully​

kill -9 $pid
wait $pid
echo $?

Expected Output:

137

Explanation: Signal 9 was sent; process terminated immediately β†’ Bash reports 137.​

9. Cheat Sheet​

Exit CodeMeaningDescription
0SuccessCommand executed successfully
1General errorNon-specific failure
2Misuse of shell builtinsSyntax or structure issue
126Command cannot executePermission or binary issue
127Command not foundMissing or invalid path
130Terminated by SIGINTManual interrupt (Ctrl+C)
137Killed by SIGKILLForceful termination (OOM or kill -9)

10. Mini-Quiz​

  1. What signal is associated with exit code 137?
  2. What is the numeric formula used to compute it?
  3. Why does the kernel invoke the OOM Killer?
  4. How can you prevent exit 137 in long-running WordPress tasks?
  5. What’s the difference between SIGTERM (15) and SIGKILL (9)?

Would you like me to continue next with Exit Status Code = 139 (Segmentation Fault / Memory Access Violation) β€” which often occurs when binaries like PHP, MySQL, or WP-CLI crash due to invalid memory access or corruption?