π Exit Status Code = 137 in Bash
π― What You Will Learnβ
- Understand what exit status code = 137 means in Bash.
- Learn how it relates to SIGKILL (signal 9) and forced termination.
- Identify system-level causes such as Out-of-Memory (OOM) kills or admin intervention.
- Distinguish
137from other signal-based codes like130(SIGINT). - Learn how to detect, prevent, and recover from 137 in WordPress VPS automation.
- Apply graceful mitigation techniques to keep long-running tasks safe.
1. 5W + 1H Frameworkβ
| Element | Description |
| What | Exit status 137 indicates that a process was killed by signal 9 (SIGKILL) β a forceful termination signal that cannot be trapped or ignored. |
| Why | It 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). |
| Who | System administrators, Bash developers, or VPS owners managing resource-intensive tasks like backups, imports, or WP cron jobs. |
| When | When the process exceeds available memory, CPU limits, or is manually force-stopped. |
| Where | Common in VPS environments, Docker containers, long-running WP-CLI commands, or large cron batch jobs. |
| How | Bash 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, andfree.
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β
| Scenario | Example | Description |
| Manual force kill | kill -9 PID | Admin or automation forcibly terminates |
| OOM Killer | mysqldump large_db | System runs out of memory |
| Docker memory cap | docker run --memory=512m ... | Container exceeds allowed RAM |
| System reboot | VPS restart mid-process | Kernel terminates running processes |
| Resource contention | Too many background jobs | Kernel selects one process to kill |
| Large file compression | tar -czf backup.tar.gz /home | Memory spike triggers OOM |
5. WordPress VPS Use Casesβ
| Use Case | Example | Explanation |
| Database dump killed | mysqldump -u root -p wordpress > backup.sql | Large DB triggers OOM kill |
| WP import/export | wp import bigfile.xml | Process exceeds PHP memory limit |
| WP cron timeout | wp cron event run --due-now | VPS kills task after CPU/RAM spike |
| Redis flush under load | redis-cli flushall | Consumes memory rapidly |
| Backup task in cron | /root/wp-backup.sh | Killed when multiple backups overlap |
| Heavy image optimization | find uploads -name '*.jpg' -exec convert {} \; | Memory exhaustion under parallel load |
6. Troubleshooting Matrixβ
| Symptom | Likely Cause | Fix |
| βKilledβ message in console | OOM Killer triggered | Add swap or increase RAM |
| Exit code 137 in cron log | Memory exhaustion | Optimize script or lower concurrency |
| Process killed instantly | kill -9 PID issued | Avoid forceful kills; use kill -15 first |
| Docker container stops | Memory limit exceeded | Raise --memory limit or optimize app |
| Random termination | VPS under heavy load | Tune MySQL, PHP-FPM, or caching settings |
7. Best Practicesβ
- Avoid forceful termination:
- Prefer
kill -15(SIGTERM) beforekill -9. - Use graceful shutdowns with
trapin scripts.
- Monitor system resources:
top
free -h
vmstat 1
- Check system logs regularly:
dmesg | grep -i killed
- Add swap memory for low-RAM VPS:
fallocate -l 1G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
- Optimize WP-CLI commands:
- Split large imports.
- Use
-skip-themesand-skip-pluginsto reduce load. - Run tasks off-peak or with background scheduling.
- Container optimization:
- Adjust Docker memory limits.
- Add
-oom-score-adjto 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 Code | Meaning | Description |
0 | Success | Command executed successfully |
1 | General error | Non-specific failure |
2 | Misuse of shell builtins | Syntax or structure issue |
126 | Command cannot execute | Permission or binary issue |
127 | Command not found | Missing or invalid path |
130 | Terminated by SIGINT | Manual interrupt (Ctrl+C) |
137 | Killed by SIGKILL | Forceful termination (OOM or kill -9) |
10. Mini-Quizβ
- What signal is associated with exit code 137?
- What is the numeric formula used to compute it?
- Why does the kernel invoke the OOM Killer?
- How can you prevent exit 137 in long-running WordPress tasks?
- 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?