[Fixed] bash: fork: retry: Resource temporarily unavailable in Linux: Step-by-Step Troubleshooting Guide

Quick Summary: The error bash: fork: retry: Resource temporarily unavailable (OS error EAGAIN) occurs in Linux when the operating system refuses to spawn a new process or thread because the current user, systemd service, or container has hit its maximum process limit. Common causes include restrictive ulimit -u (nproc) thresholds, systemd’s default TasksMax cap, runaway thread-spawning applications, or Docker container pids-limit restrictions.

Understanding the Root Causes

In Linux, every user-space process and kernel thread counts toward per-user and system-wide task limits. When a shell or application calls fork() or clone() and the active task count hits the configured ceiling, the kernel rejects the allocation with EAGAIN. The primary triggers include:

  • Per-User Process Limits (RLIMIT_NPROC): Default nproc limits in /etc/security/limits.conf or /etc/security/limits.d/ capping a service user (such as www-data, mysql, or jenkins) at 1024 or 4096 threads.
  • Systemd TasksMax Restrictions: Modern Linux distributions running systemd enforce a per-service TasksMax ceiling (often 15% of system memory/pid_max or a fixed 4915 tasks), blocking multi-threaded Java, Go, or Node.js services.
  • Runaway Processes or Zombie Leaks: A misconfigured cron job, recursive script, or hung worker spawning thousands of idle threads until the limit is saturated.
  • Container PID Limits: Docker or Kubernetes cgroups enforcing a tight --pids-limit on a container running a multi-worker application.

Step 1: Quick Triage – Inspect Active Threads and Limits

Identify whether the issue is caused by a runaway process or an overly tight user/service limit. Note that if Bash cannot fork new commands, you can still run shell built-ins like ulimit and echo:

# 1. Check your current user's max user processes limit (shell built-in)
ulimit -u

# 2. Check current number of processes and threads per user (run from root/another session if blocked)
ps -eLo user= | sort | uniq -c | sort -nr | head -n 10

# 3. Inspect the system-wide maximum PID limit
cat /proc/sys/kernel/pid_max

# 4. Find which specific process is spawning the highest number of threads
ps -eo nlwp,pid,user,cmd --sort=-nlwp | head -n 15

Step 2: Increase User nproc Limits in limits.conf

If a legitimate workload (such as a database, JVM, or CI runner) requires more threads than the default user limit allows, raise the soft and hard nproc limits:

# 1. Temporarily raise the limit in the current shell session
ulimit -u 65535

# 2. Make the change permanent in /etc/security/limits.conf (or a drop-in in /etc/security/limits.d/)
sudo tee /etc/security/limits.d/99-nproc.conf <<EOF
*          soft    nproc     65535
*          hard    nproc     65535
root       soft    nproc     unlimited
EOF

# Note: On RHEL/CentOS, also check /etc/security/limits.d/20-nproc.conf which can override limits.conf

Log out and log back in (or restart the affected session) for PAM to apply the new limits.d configuration.

Step 3: Raise Systemd TasksMax and Docker PID Limits

Services managed by systemd ignore limits.conf for task counts and use TasksMax instead. Similarly, containerized apps use cgroup PID limits:

# Option A: Increase TasksMax for a specific systemd service (e.g., docker, mysql, or app.service)
sudo systemctl edit my-app.service

# Add the following override block:
[Service]
TasksMax=infinity
LimitNPROC=65535

# Reload systemd and restart the service
sudo systemctl daemon-reload
sudo systemctl restart my-app.service

# Option B: Increase Docker container PID limit if failing inside a container
docker run -d --pids-limit=4096 --name my-app my-image:latest

Verification & Testing Steps

Confirm that the updated limits are active for your user session or systemd service and verify that new processes spawn cleanly:

# 1. Verify active user process limit
ulimit -u
# Expected output: 65535

# 2. Check the live limits of a running systemd service
systemctl show my-app.service -p TasksCurrent -p TasksMax

# 3. Inspect kernel process limits directly for a specific PID
cat /proc/$(pgrep -n my-app)/limits | grep -i "max processes"

Troubleshooting Summary Comparison

Environment / Symptom Primary Root Cause Recommended Solution
Interactive SSH / Cron Job Low per-user ulimit -u (nproc) cap Raise nproc in /etc/security/limits.d/99-nproc.conf
Systemd Daemon (Java / DB / Web) Service hit systemd TasksMax ceiling Set TasksMax=infinity via systemctl edit
Docker / Kubernetes Container Cgroup pids.max limit reached inside container Increase --pids-limit or inspect thread leaks
Sudden Exhaustion by Single App Thread leak or recursive fork bomb Identify top thread consumer via ps -eo nlwp,pid,cmd

Leave a Reply