fork, exec, exit codes and environment
Unix creates a process in two steps: copy yourself, then replace the copy with a new program. Learn the steps, and what the exit status really carries.
fork() creates a child that is a near-copy of the parent: same memory contents, same open descriptors, same environment. It returns twice, once in each process. execve() then replaces the child's program with a new one while keeping its open descriptors. Between the two, the child can rearrange descriptors, change directory or drop privileges. That gap is how a shell sets up redirections and pipes.
From one process to a running command
waitpid. Until it does, the dead child lingers as a zombie.Exit codes
A process exits with a status from 0 to 255. Zero means success; anything else is failure by convention. If a signal kills the process, there is no exit code. The parent sees the signal number instead, and shells report it as 128 plus the signal: 137 is SIGKILL, often from the OOM killer, and 143 is SIGTERM.
| Status | Usually means |
|---|---|
| 0 | Success |
| 1 | General failure |
| 2 | Misuse of a shell builtin or bad arguments |
| 126 | Found but not executable |
| 127 | Command not found |
| 130 | Interrupted by SIGINT (Ctrl-C) |
| 137 | Killed by SIGKILL, often out of memory |
| 143 | Terminated by SIGTERM |
Environment variables
The environment is a list of KEY=value strings copied into each child at exec time. Changing it later in the parent does not affect running children. Children inherit secrets too: if the harness has GITHUB_TOKEN in its environment, every command the model runs can read it unless you pass an explicit, filtered environment.
import { spawn } from "node:child_process";
const SAFE_ENV = ["PATH", "HOME", "LANG", "TERM"];
const child = spawn("bash", ["-lc", command], {
cwd: workspace,
// Pass an allowlist, never process.env wholesale
env: Object.fromEntries(SAFE_ENV.map((k) => [k, process.env[k] ?? ""])),
stdio: ["ignore", "pipe", "pipe"],
});
child.on("exit", (code, signal) => {
// Exactly one of these is non-null
report(signal ? `killed by ${signal}` : `exited with ${code}`);
});Node's exit event gives code or signal. Report both, and the model can tell a test failure from an OOM kill.
Where you will meet it
A job that ends with status 137 and no error message was usually killed by the kernel or the container runtime for exceeding its memory limit.
Agent harnesses strip tokens from child environments and inject credentials through a proxy instead (Module 12).
Modern runtimes often use posix_spawn or vfork instead of fork so a large parent does not pay to copy its page tables.
Check yourself
Answer out loud before you open each one.
What survives an execve call?
The PID, open descriptors not marked close-on-exec, the working directory and credentials. Memory and code are replaced.
A command exits with 137. What happened?
It received SIGKILL (128 + 9), commonly from the OOM killer or a hard timeout.
Why pass an explicit environment to model-run commands?
Children inherit the whole environment, including any secrets the harness holds.
Try it
Run bash -c 'sleep 30' & sleep 1; kill -TERM $!; wait $!; echo $?. Then repeat with kill -KILL. Explain both numbers.