📚 Encyclopedia

Tools & Workflow · updated 2026-10-26

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

What works

  1. 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 & disown The 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.
  2. 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 with systemctl --user status <unit>.
  3. 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.
  4. 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

See also

Linked from

Sources: keyboard memory (history), message memory (quarantine_2026_09), tldr memory (feedback_detached_long_deploys, release-v1.31.0-audit, feature-ai-default-model, feature-merch-store, feature-ai-model-discovery), posts #1445, #1631, #1688, #1695, tldr v1.62.0