Running long jobs so they survive the AI session that started them
setsidnohupsystemd-runcgroupcronclaude-codedeploy
A Claude Code session (interactive or headless) is a poor parent for anything that runs longer than a turn. Background commands started through the harness are reaped at turn or session teardown, and processes started by a headless run live inside the service's cgroup. The symptom is always the same: a log that simply stops, with no error. It looks like a hang; it is a kill.
What does not work
- Harness
run_in_background,nohup cmd &inside such a call, or a tracked watch loop: killed at teardown. A 10+ minute./deploy.shdied right after "Built build/web". setsid nohupfrom a headless claude-service run: survives the session, but it is still in theclaude-service.servicecgroup, sosystemctl --user restart claude-servicekills it. (A crypto-miner node launched this way blocked a needed service restart for two weeks.)- A detached
while sleeploop for a multi-day wait: dies on the next desktop reboot.
What works
- Short to medium jobs (a deploy): write a runner script and fully detach it:
bash setsid nohup /tmp/runner.sh >/dev/null 2>&1 < /dev/null & disownThe runner does the work, verifies it, and reports back on its own: e.g.deploy.sh, then the smoke test, then post the result (exit codes, live version, log tail) somewhere the human will see it. For TLDR that is~/dev/claude-service/tldr-followup.sh <postId> "markdown", which adds a follow-up response and sends a push. - Anything that must outlive a service restart: give it its own transient unit:
systemd-run --user --unit=tldr-deploy-1690 /tmp/runner.sh. It is no longer in the parent's cgroup. Check withsystemctl --user status <unit>. - Waiting hours or days for an event: use cron (a single-shot check every N minutes guarded
by
flock), not a detached loop. Cron survives reboots; make the script remove its own crontab line when done. - Interactive session that wants the result: have the runner write a done-file
(
echo "deploy_rc=$RC smoke_rc=$RC2 live=$LIVE" > /tmp/x.done) and arm a Monitor/until-loop on that file with a long timeout. Also detect "runner died without a done-file" (pgrep -f runner || { tail log; exit 1; }).
Never restart the service you are running inside
A headless claude-service run is a child of claude-service. systemctl --user restart
claude-service from inside it kills the run mid-turn. It has happened (an attempt edited the
service, restarted it, and died with the work uncommitted; the retry found it running but not in
git). Options: hot-reload config where the service supports it (POST /admin/reload-projects),
schedule the restart from a separate transient unit that waits until the service is idle, or leave
the restart to the human.
Retried runs inherit leftovers
When a headless job is retried after a timeout, assume attempt 1 may have done part of the work:
check git worktree list (or the project's ./worktree.sh list), git log, and live state before
redoing anything. Seen: an uncommitted but complete diff left in a worktree; a deploy that had
finished before the timeout fired.
Detached Claude builders and the usage limit
A detached claude -p builder (a whole app built unattended) shares the account's usage limit with
everything else. On the CNC rebuild (post #1662, 2026-09) the first builder died after 14 minutes on
"session limit · resets 12:30am", and two later runs each burned a full 5-hour window in about 15
minutes because the builder fanned out parallel subagents. The work up to that point landed only
because it had been committed. What made later builders reliable:
- A wrapper script that watches the output for the limit message, posts a "paused until HH:MM"
update, sleeps until the reset, and resumes with claude -p --continue (capped at, say, 12 cycles).
- A brief that says: no subagents, work serially, commit every ~10 minutes, and do the
user-visible deliverable (the page, the deploy) before polish.
- One builder per job; check pgrep -af <job-id> and the job's progress file before starting a second.
The builder recipe as used for Keyboard and Spectre (posts #1718 to #1751)
TLDR AI posts have a 15-minute session limit, so any "do all" request is handed off within the post:
- systemd-run --user --unit=tldr-<post>-builder /tmp/tldr-<post>/run.sh, with the spec in brief.md,
a fixed session id in session_id (so retries resume the same conversation), logs in run<N>.log,
and a DONE marker written at the end. The desktop has no uuidgen; use
python3 -c "import uuid;print(uuid.uuid4())". A launch with a missing brief misfired once.
- The brief names the branch, target versionCode, and decisions already made, and tells the builder to
report through one TLDR follow-up and append its own memory entry.
- To queue a second builder behind a busy one, make its wrapper sleep until the first one's DONE
exists or its unit is inactive.
- New requests for the same repo while a builder runs: if the builder has no commits yet, stop it,
amend the brief, relaunch with a fresh session id. Once it has commits, do not stop it; fix on main in
a separate git worktree (copy the gitignored signing files in) and let the builder merge main
before releasing. A brief that says "check git log main and merge before release" makes this safe.
- Before assuming a builder's release is live, check the DONE file, git log, and the live
version file.
- A headless timeout is not lost work. A run killed at the CLI's 900 s limit three times had kept
committing, and its detached deploy hook finished the release after the CLI died. Commit early,
detach the deploy, and check git log and the live version before redoing anything.
- Wrap git push in timeout 90 inside chained release commands; one push to GitLab hung for 10
minutes with no output and stalled everything after it.
- An a && b <<EOF chain skips the heredoc when a fails.
Gotchas
pgrep -f deploy.shmatches your own shell if the pattern appears in your command line, and it also matches other projects'deploy.shruns: many repos have one, and on 2026-10-23 (tldr v1.62.0) a watcher hung on another session's deploy. Wait on the PID ($!right aftersetsid) or on the log's completion line (e.g.Deployed tldr vX.Y.Z), never on a generic process name.- Prefer the runner posting its own result over the session polling for it; the session may be gone by the time the job ends.
See also
- Release-directory deploys with an atomic symlink swap
- claude-service (ProcrastinAI's webhook runner for headless Claude)
- Running several AI coding sessions on one repo (worktrees, locks, fresh memory)
Linked from
- Building a proof-of-work cryptocurrency from scratch (BiteCoin design notes)
- claude-service (ProcrastinAI's webhook runner for headless Claude)
- Exporting Google Photos with Takeout and restoring metadata with ExifTool
- Lifecycle of an async AI request (status machine, expiry, retries, failure)
- Long autonomous AI builds (vacation runs and scheduled free time)
- Release-directory deploys with an atomic symlink swap
- Running several AI coding sessions on one repo (worktrees, locks, fresh memory)
- Shell and process gotchas for agent sessions on the desktop