bash: fork: retry: Resource temporarily unavailable
fork returned EAGAIN because a process or thread ceiling was hit. Identify whether nproc, cgroup pids, TasksMax or threads-max is the limit.

Introduction & Quick Summary
bash: fork: retry: Resource temporarily unavailable
bash: fork: retry: Resource temporarily unavailable
bash: fork: Resource temporarily unavailablefork() returned EAGAIN, and bash retries before giving up. The system has a hard ceiling on processes or threads for your user, your cgroup, or the kernel, and it has been reached. Memory usually is not the problem, even though the second variant says Cannot allocate memory.
Four ceilings produce this exact message, and you have to identify which one you hit:
RLIMIT_NPROC(ulimit -u) for the user account, counted per UID across all sessions.- cgroup
pids.maxin containers and systemd slices, which counts processes and threads together. kernel.threads-maxandkernel.pid_max, the machine-wide limits.vm.max_map_countor address space, which give theCannot allocate memorywording with the same visible symptom.
| Where you see it | Likely limit | Check command |
|---|---|---|
| Shell in a container | cgroup pids.max | cat /sys/fs/cgroup/pids.max |
| Interactive SSH session | ulimit -u | ulimit -u, ulimit -Hu |
| Service under systemd | TasksMax | systemctl show -p TasksMax <unit> |
| Entire host saturated | threads-max | sysctl kernel.threads-max |
| CI job after many parallel steps | Per-user nproc | ps -eLf --no-headers | wc -l |
Core Mechanics, Signatures & Reproducible Setup
fork() fails with EAGAIN when the kernel cannot allocate a task structure, when the calling user already has RLIMIT_NPROC tasks, or when the cgroup's pids.max would be exceeded. Since Linux 2.6.23 threads are tasks too, so a JVM or Node process with hundreds of threads consumes hundreds of slots from the same counter. That is why the trigger is often an application, not a runaway shell loop.
Read every limit at once:
ulimit -u # soft nproc
ulimit -Hu # hard nproc
cat /proc/sys/kernel/threads-max
cat /proc/sys/kernel/pid_max
cat /sys/fs/cgroup/pids.max 2>/dev/null || true
cat /sys/fs/cgroup/pids.current 2>/dev/null || true
systemctl show -p TasksMax -p TasksCurrent user.sliceReproduce it in a shell without harming the host:
bash -c 'ulimit -u 40; for i in $(seq 1 200); do sleep 30 & done; wait'
# bash: fork: retry: Resource temporarily unavailableCount what is actually consuming tasks:
ps -eLf --no-headers | wc -l # total threads
ps -eLf --no-headers | awk '{print $1}' | sort | uniq -c | sort -rn | head
ps -eo user:20,nlwp,pid,comm --sort=-nlwp | head -15The last command is the fastest way to find a single process holding thousands of threads, which is common with misconfigured connection pools and thread-per-request servers.
Key Comparison Table
| Limit | Scope | Counts threads | Raised at runtime | Persistence |
|---|---|---|---|---|
ulimit -u (soft) | User | Yes | ulimit -u N | Shell only |
ulimit -Hu (hard) | User | Yes | Root only | /etc/security/limits.conf |
cgroup pids.max | Container or slice | Yes | systemctl set-property | Unit drop-in |
TasksMax | systemd unit | Yes | systemctl set-property --runtime | Drop-in file |
kernel.threads-max | Host | Yes | sysctl -w | /etc/sysctl.d |
kernel.pid_max | Host | PIDs only | sysctl -w | /etc/sysctl.d |
| Environment | Default ceiling | Where to change |
|---|---|---|
| Docker container | Inherits host, often unlimited | --pids-limit, Compose pids |
| Kubernetes pod | Node allocatable, no pod limit by default | --max-pods, kubelet podPidsLimit |
| systemd service | DefaultTasksMax (15% of threads-max) | TasksMax= in unit |
| SSH login shell | nproc from limits.conf | /etc/security/limits.d/ |
| GitHub Actions runner | Host nproc | Not configurable |
Practical Edge Cases, Error Fixes & Deep Dive Code Examples
Case 1: A container with --pids-limit set
docker run --rm --pids-limit=100 alpine sh -c \
'for i in $(seq 1 200); do sleep 30 & done; wait'
# sh: can't fork: Resource temporarily unavailableRaise the limit or, better, find the leak. A JVM started without -XX:ActiveProcessorCount and -Xss tuning can spawn more threads than a small pids.max allows:
services:
api:
image: app:latest
pids_limit: 512Case 2: systemd service hitting TasksMax
systemctl show -p TasksMax -p TasksCurrent myservice.service
sudo systemctl edit myservice.service[Service]
TasksMax=4096DefaultTasksMax on modern systemd is 15% of kernel.threads-max, so a busy machine effectively gives each service a moving target. Set it explicitly for servers that manage their own thread pools.
Case 3: The nproc limit that does not match limits.conf
PAM applies limits at login only if pam_limits.so is configured for the service. A unit started by systemd does not read limits.conf; it reads LimitNPROC in the unit. This is the single most common reason a documented ulimit change has no effect:
[Service]
LimitNPROC=65535Case 4: Thread leaks in Java and Node
jcmd 1 Thread.print | grep -c '"' # thread count from a JVM
node -e "console.log(process._getActiveHandles().length)"A JVM at its thread ceiling usually reports java.lang.OutOfMemoryError: unable to create native thread in logs at the same time as the shell error. The fix is pool sizing, not a bigger limit.
Case 5: Zombie processes and Cannot allocate memory
A parent that never calls wait() leaves zombies, which hold PID slots but no memory.
ps -eo stat,ppid,pid,comm | awk '$1 ~ /^Z/ {print}' | headZombies disappear when the parent exits or reaps. A container whose PID 1 is a shell script often accumulates them; use exec or tini so signals and reaping work.
Case 6: The host-wide ceiling during a fork storm
sysctl kernel.threads-max kernel.pid_max vm.max_map_count
grep -c ^processor /proc/cpuinfoIf ps -eLf | wc -l is near threads-max, no per-user change helps. Find the offender and cap it, then raise threads-max only as a stopgap.
Common Compilation/Runtime Mistakes & Version Gotchas
Cannot allocate memoryis misleading. It appears forEAGAIN, not onlyENOMEM; bash uses the same wording for both. Do not start by adding RAM.ulimit -uin a container shows the host value. cgroup v2pids.maxcan still be the lower ceiling, so check both.RLIMIT_NPROCis per UID, not per process tree. Two services running as the same user share one budget, which is why moving a service to its own user often fixes it.- cgroup v1 versus v2 paths.
pids.maxlives at/sys/fs/cgroup/pids/pids.maxon v1 and/sys/fs/cgroup/<path>/pids.maxon v2; a script that reads only one path silently reports nothing. LimitNPROChas no effect on root. Root bypassesRLIMIT_NPROCin many kernels, so a root shell appears unlimited while application users are constrained.- Fork bombs from build tools. Parallel
make -j,jest --maxWorkers=100%, orxargs -Pon a 64-core host can exceed a per-user limit intentionally set for interactive use. Cap parallelism rather than raising limits.
When the failing command is wrapped by a script that hides the real error text, log the full stderr and compare a working and failing run with the diff tool. If the limit is being applied by a container runtime, the Docker Compose viewer shows whether pids_limit or deploy.resources is set where you think it is. For confirming the exact numeric ceilings configured on a host, a small cron entry checked through the cron expression tool can sample ps -eLf | wc -l before an incident rather than after.
Primary references: fork(2) lists EAGAIN and its causes, and the kernel sysctl documentation covers threads-max and pid_max.
Official references
primary documentationThe Linux documentation that defines the behaviour this guide explains.
Try matching browser tools
All toolsSmall utilities that fit Linux work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Linux guides
All Linux guidesnginx 502 Bad Gateway: upstream connection refused
The log line names the address and errno, which is the diagnosis. Fix the host, the start-up order, the buffers, or the timeout that actually fired.
systemd status=203/EXEC and 217/USER: Exec and Permission Failures
203 is a failed execve, 217 is a user systemd could not resolve. Both happen before your program runs, and the check that finds each takes one command.
Linux Archiving and Compression: tar, gzip, xz, and zstd Syntax and Trade-offs
tar archives and the compressors only shrink the stream. Argument order, -C path handling, ownership and xattr preservation, streaming over SSH, and.
apt: NO_PUBKEY and signatures couldn't be verified
The repository key is missing, expired or unpinned. Fetch the vendor key into a dedicated keyring and reference it with signed-by.
More in Linux
All Linux guidesThe guides filed next to this one in Linux, in library order.