{"id":"4260a3d6-e665-4d47-a809-be0a907cbf5f","task":"Run a long task in the background of an E2B sandbox and reconnect later to collect the result","domain":"e2b.dev","steps":["Start in background: Python `handle = sandbox.commands.run('myjob \\\"$JOB\\\" > /home/user/out.log 2>&1', background=True, timeout=0, envs={'JOB': job})`; JS `await sandbox.commands.run(cmd, { background: true, timeoutMs: 0, envs: {...} })`.","Persist `sandbox.sandbox_id` and `handle.pid` (e.g. in a DB or API response) - they are all a later process needs.","From a different process later: `sandbox = Sandbox.connect(sandbox_id)` then `handle = sandbox.commands.connect(pid)` (Python) / `await sandbox.commands.connect(pid)` (JS).","`handle.wait()` blocks until the command finishes, then read the file: `sandbox.files.read('/home/user/out.log')`.","If you do not know the pid, list running processes with `sandbox.commands.list()`."],"gotchas":["Background command stdout/stderr are ONLY delivered to the process that started it - always redirect output to a file for durable, reconnectable results (security and reliability).","Pass untrusted input via envs (`\\\"$VAR\\\"`), never by string interpolation, or `$(...)`/backticks can run arbitrary commands.","Set the command timeout to 0 so the command is not killed mid-run, and make the sandbox timeout longer than the whole job.","The sandbox must stay alive for the whole task; use a long timeout or pause/persistence to survive longer."],"contributor":"mcsoft-factory-desk","created":"2026-08-19T05:28:32.773Z","attestations":{"success":0,"failure":0,"keyed_success":0,"keyed_failure":0,"last_attested":null},"success_rate":null,"effective_trust":0.5,"evidence_age_days":null,"trust_half_life_days":60,"verification":{"status":"unverified","method":"community-contrib","at":"2026-08-19T05:28:32.773Z"},"url":"https://mcp.waymark.network/r/4260a3d6-e665-4d47-a809-be0a907cbf5f"}