Disentinel/grafema / .claude/skills/docker-exec-background-process
docker-exec-background-process
VerifiedFix docker exec hanging when starting background processes (servers, daemons) inside containers. Use when: (1) docker exec never returns despite using & or nohup, (2) background server started in container causes docker exec to hang indefinitely, (3) env_startup_command in SWE-bench or similar frameworks times out, (4) setsid/disown needed for proper process detachment in Docker. Root cause: docker exec tracks ALL processes in the exec session, not just the top-level PID.
Docker Exec Background Process Detachment
Problem
When using docker exec to start a background process (like a database server) inside a
container, the docker exec command hangs indefinitely and never returns, even when using
&, nohup, or output redirection.
Context / Trigger Conditions
docker exec container bash -c "my-server &"never returnsdocker exec container bash -c "nohup my-server > /dev/null 2>&1 &"still hangs- Startup commands in frameworks (SWE-bench, mini-SWE-agent) time out at 300/600s
- Any scenario where
docker execneeds to start a long-running daemon and return
Root Cause
Docker exec tracks ALL processes started in the exec session, not just the top-level bash process. This is different from regular bash behavior:
&— creates background job but bash still tracks it in its job tablenohup— prevents SIGHUP but does NOT create a new session; docker still tracks it- Output redirection (
>/dev/null 2>&1) — prevents output but docker exec still waits for all session processes to terminate
Docker exec uses Linux process sessions/groups. All processes started within an exec call share the same session. Docker waits for the entire session to finish.
Solution
Use setsid + disown + stdin/stdout/stderr redirection:
docker exec container bash -c 'setsid /path/to/server arg1 arg2 </dev/null >/dev/null 2>&1 & disown && sleep 1 && echo "Ready"'
All four components are required:
| Component | Purpose |
|---|---|
setsid | Creates a NEW session (new session ID, detaches from exec session) |
</dev/null | Prevents stdin inheritance from docker exec |
>/dev/null 2>&1 | Prevents stdout/stderr from keeping pipe fds alive |
& disown | Removes job from bash's job table so bash doesn't wait for it |
Why each is necessary:
- Without
setsid: Process stays in docker exec's session → docker waits - Without
</dev/null: Process may hold stdin fd → docker waits - Without
>/dev/null 2>&1: Process may hold pipe fds (especially with| tail) → docker waits - Without
disown: Bash waits for background jobs before exiting → docker waits
Related Problem: Pipe FD Inheritance
A compounding issue occurs when using pipes in the startup command:
# THIS HANGS: tail waits for EOF, but server inherits the pipe fd
docker exec container bash -c 'my-server 2>&1 | tail -3'
If my-server spawns child processes with stdio: 'inherit' (common in Node.js), those
children inherit the pipe file descriptor. tail waits for ALL writers to close the pipe,
but the inherited fd in the child process keeps it open forever.
Fix: Don't pipe server output. Use >/dev/null 2>&1 or write to a log file:
# Instead of piping, redirect to file and read separately
docker exec container bash -c 'my-server > /tmp/server.log 2>&1 & sleep 2 && tail -3 /tmp/server.log'
Verification
After running the startup command:
docker execshould return promptly (within thesleepduration)docker exec container ps auxshould show the server process still running- The server should be accessible (check socket, port, etc.)
Example: Starting rfdb-server in SWE-bench
# mini-SWE-agent config
run:
env_startup_command: |
ln -sf /opt/grafema/node_modules/.bin/grafema /usr/local/bin/grafema && \
cp /opt/node_modules/@grafema/rfdb/prebuilt/linux-x64/rfdb-server /usr/local/bin/rfdb-server && \
chmod +x /usr/local/bin/rfdb-server && \
setsid /usr/local/bin/rfdb-server /testbed/.grafema/graph.rfdb \
--socket /testbed/.grafema/rfdb.sock </dev/null >/dev/null 2>&1 & disown && \
sleep 2 && echo "Server ready"
What Does NOT Work
# FAILS: nohup doesn't create new session
docker exec container bash -c 'nohup my-server &'
# FAILS: output redirection alone isn't enough
docker exec container bash -c 'my-server > /dev/null 2>&1 &'
# FAILS: disown alone isn't enough (still in same session)
docker exec container bash -c 'my-server & disown'
# FAILS: even all three without setsid
docker exec container bash -c 'nohup my-server > /dev/null 2>&1 & disown'
Notes
setsidis available on most Linux distributions (part of util-linux)- The
sleepafterdisowngives the server time to initialize before the script continues - If the server needs to be verified running, check its pidfile or socket after the sleep
- On macOS Docker Desktop, x86_64 emulation adds ~10x latency to all operations
- For SWE-bench/mini-SWE-agent,
env_startup_commandruns viadocker exec bash -c "...", so this pattern applies directly
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| writing-skillsobra/superpowersUse when creating new skills, editing existing skills, or verifying skills work before deployment | Stars: 319k | Updated | Claude CodeCodex | Verified | Install |
| xlsxanthropics/skillsUse this skill any time a spreadsheet file is the primary input or output. This means any task where the user wants to: open, read, edit, or fix an existing .xlsx, .xlsm, .xltx, .csv, or .tsv file (e.g., adding columns, computing formulas, formatting, charting, cleaning messy data); create a new spreadsheet from scratch or from other data sources; or convert between tabular file formats. Trigger especially when the user references a spreadsheet file by name or path — even casually (like "the xlsx in my downloads") — and wants something done to it or produced from it. Also trigger for cleaning or restructuring messy tabular data files (malformed rows, misplaced headers, junk data) into proper spreadsheets. The deliverable must be a spreadsheet file. Do NOT trigger when the primary deliverable is a Word document, HTML report, standalone Python script, database pipeline, or Google Sheets API integration, even if tabular data is involved. | Stars: 196k | Updated | Claude CodeCodex | Verified | Install |
| shipping-and-launchaddyosmani/agent-skillsPrepares production launches. Use when preparing to deploy to production, or when asking what needs to be in place before shipping. Use when you need a pre-launch checklist, when setting up monitoring, when planning a staged rollout, or when you need a rollback strategy. | Stars: 118k | Updated | Claude CodeCodex | Verified | Install |
| childhoodthedotmack/claude-memBabysit a shipped release after merge, publication, or deployment. Verify published packages, production behavior, installed plugins, and late CI or user reports until the observation window is clean; use babysit for PR readiness before release. | Stars: 117k | Updated | Claude CodeCodex | Verified | Install |