Skip to content
Back to skills

Process Discipline

DSecurity

Long-running and background processes. Load before starting a server, watcher, port-forward, polling loop, tmux session, test tmux server, or any run_in_background job or async agent, and before stopping, reaping or relaunching a process. Holds the no-disown rule, the heartbeat recipe, Codex rollout watching, localhost binding, fail-fast polling, launch-parent linkage, the tmux -L rule, and reaping by dead owner, never by age.

  • 14 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added November 29, 2026
developmentpythongorubyshellbashnodefastapitestinggitapi

Works with

  • claude code
  • terminal
  • api
  • mcp

Security analysis

D50/100
  • criticalPipes output to a shell interpreter
  • criticalPipes output to a shell interpreter
  • criticalSends environment variables or credentials to an external URL

Pro shows the line behind each finding and how to fix it

Scanned December 1, 2026

npx -y skills add poma-ai/megavibe --skill process-discipline --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Process Discipline?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Process Discipline
[![Security: D — Skills Directory](https://www.skillsdirectory.com/api/skills/poma-ai-process-discipline/badge)](https://www.skillsdirectory.com/skills/poma-ai-process-discipline)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: process-discipline
description: Long-running and background processes. Load before starting a server, watcher, port-forward, polling loop, tmux session, test tmux server, or any run_in_background job or async agent, and before stopping, reaping or relaunching a process. Holds the no-disown rule, the heartbeat recipe, Codex rollout watching, localhost binding, fail-fast polling, launch-parent linkage, the tmux -L rule, and reaping by dead owner, never by age.
---

# Long-running processes

When you start something that outlives the current command — a server, watcher, port-forward, or polling loop — these rules apply. Getting them wrong leaves orphan processes running for days or weeks under launchd, exposing services on the LAN, or hammering external APIs in failure loops.

## Don't disown to launchd

If a process ends up parented to PID 1 (launchd on macOS, init/systemd on Linux), nothing will catch it. The shell that launched it is gone. The user has no terminal to come back to. It runs forever.

Avoid:
- `nohup ... &`
- Bare `&` in a shell that's about to exit — this is the common case, because shells spawned by tool calls return immediately
- `disown` after backgrounding
- `setsid`, `screen -dm`, anything that detaches by design

Use one of these instead:
- **Bash `run_in_background`** (Claude Code's own backgrounding) — bound to the session, cleaned up when the session ends. Right for short-lived work the parent will check on.
- **Foreground in a terminal the user is watching** — for things you want them to see in real time.
- **Named tmux session** — for things genuinely meant to outlive the current Claude session: `tmux new -d -s <project>-<purpose> '<command>'`. The session name is how the user finds and kills it later.

**A Claude session you start from a session carries its parent.** `megavibe` records every launch in `~/.megavibe/launches/<pid>.json` with the session that started it, so a session viewer can nest the child under its parent. A launch that is your own child process (from your Bash tool) needs nothing: the wrapper uses `CLAUDE_CODE_SESSION_ID` while an ancestor process is that session's registered process (`~/.claude/sessions/<pid>.json` names that id). A launch that is not your child but runs on this machine (tmux, a LaunchAgent) must say whose it is: prefix it with `MEGAVIBE_PARENT_SESSION=$CLAUDE_CODE_SESSION_ID`, as in `tmux new -d -s <name> env MEGAVIBE_PARENT_SESSION=$CLAUDE_CODE_SESSION_ID megavibe ...`. Expand the variable in your own shell (unquoted or in double quotes), never inside a single-quoted tmux command string: tmux runs that string in the server's environment, which can hold a stale session id. `megavibe node session start` sets it for the windows it opens, so use that for repo sessions on a node instead of a bare tmux call. The linkage is same-machine only: a parent on another host (ssh) is not in the local registry, so do not set it there. A helper meant to outlive your session gets `MEGAVIBE_PARENT_SESSION=` (empty), or it is reported as an orphan once you end. Without it the child shows up unrelated (a stale id a tmux server handed on is ignored, not believed). `MEGAVIBE_PARENT_SESSION=` (empty) means no parent.

**A throwaway tmux server for a test needs `-L`, not `TMUX_TMPDIR`.** Inside tmux, `$TMUX` names the live server's socket and tmux uses it in preference to `TMUX_TMPDIR`, so `export TMUX_TMPDIR=$(mktemp -d); tmux kill-server` kills the LIVE server and every session on it. Use `env -u TMUX tmux -L <name> ...` for every command, with a literal name (not `$$` or another variable: the guard cannot see what it expands to), and stop it only with the same `-L <name>`; `block-dangerous-bash.sh` allows a tmux kill verb only with a literal `-L` name other than `default` that is not the live socket (`-S` is always blocked).

## Backgrounded work gets a heartbeat

`run_in_background` and an async `Agent` return at once and promise a
notification when the task completes. A task that hangs, or restarts itself in
a loop, never completes — so the notification never comes, and from the main
thread a crashloop is indistinguishable from progress. A completion event is not monitoring.

The moment you start one, arm a `Monitor` heartbeat on its output file and do
not end the turn "waiting":

```bash
f="<output file>"; last=0; while sleep 240; do s=$(wc -c <"$f" 2>/dev/null | tr -d ' ' || echo 0); printf 'bg <id>: %s bytes (+%s) | %s\n' "$s" "$((s-last))" "$(tail -c 200 "$f" 2>/dev/null | tr '\n' ' ')"; last=$s; done
```

Every beat wakes you with fresh evidence, whether or not the task finished.
Read it against what the task *is*, not as a verdict: a build or test run at
`+0` for two beats is hung, while a server idling at `+0` is healthy; a file
that keeps growing with the same lines repeating is a crashloop; a subagent
transcript whose last tool names repeat for many beats is a loop. Read the
output before acting, then act — kill it, fix it, `SendMessage` or `TaskStop`
the agent — instead of waiting one more interval. When the completion
notification arrives, `TaskStop` the heartbeat. There is no exemption for
"short" tasks: a thirty-second task that hangs is the same silence, and the
heartbeat costs one notification if the task finishes first. If `Monitor` is
not in your tool set, `ToolSearch "select:Monitor"`; if it is unavailable
altogether, run the same loop with a fixed beat count through
`run_in_background` and let its completion be the wake-up.

**A Codex job is watched through its rollout, not its output file.** Its stdout is usually redirected, so the task's `.output` file is empty, and `-o FILE` holds only the final message. The progress log is `~/.codex/sessions/YYYY/MM/DD/rollout-*.jsonl`; `~/.megavibe/scripts/codex-progress.sh --cwd <its -C dir> --brief` prints its size, idle time and last assistant message, and `watch-background.sh` builds the heartbeat loop from it for a background command that starts Codex. Rollout bytes at +0 are a reason to read the idle seconds and the last message, not proof of a hang (reasoning gaps of minutes occur): hung is idle past about 10 minutes or a dead process. Detail: the `delegation` skill, "Watching a Codex job".

`watch-background.sh` enforces both halves. On start it injects the exact loop
to arm (to a subagent's call, identified by `agent_id`, the 170 s wait-slice advice instead). On a turn boundary it reads the harness's live task registry and
refuses to let the turn end while a running task older than the interval has
no other running task whose command names it — which is what an armed
heartbeat looks like, and which stops being true the moment the heartbeat
exits or is stopped. Once per interval, all overdue tasks in one reason, never
in a loop. A Stop hook cannot wake you later; the heartbeat is the only thing
that can. `MEGAVIBE_BG_CHECK_SECS` sets the interval (clamped to 60–600 s; the
heartbeat beats 60 s under it); `MEGAVIBE_BG_WATCH=0` switches the hook off.
The main thread's cache usually lives 1 hour (5 minutes on API billing or when forced), so the default four-minute beat is cheap there and not a reason to beat faster. A **subagent's** cache expires after 5 minutes
by default, so every wait past 5 minutes re-writes the whole context at 1.25x
instead of reading it at 0.1x: a subagent never waits longer than 170 s in one
tool call. See the `spinouts` skill for the measurement and the wait recipe. Do not read a
subagent's `.output` symlink whole: it is the full JSONL transcript. A subagent's result can arrive while background work it launched still runs: once its result is used, stop only a test server or watcher its own task started (never a job that writes or deploys, nor one another session or the user relies on; if unsure, ask) with `TaskStop` by task id; keep any server a remaining verification step needs, and list running tasks before declaring done.

## Bind dev servers to localhost by default

For uvicorn, fastapi, `python -m http.server`, vite, next dev, webpack-dev-server, json-server, and similar: pass `--host 127.0.0.1` (or the framework equivalent) unless the user explicitly asked for LAN access.

`--host 0.0.0.0` is uvicorn's default. It is also `python -m http.server`'s default. That puts the service on every interface, IPv4 + IPv6. On a coworking, hotel, or conference WiFi that's an unauthenticated read of your project directory or local API. The user usually didn't ask for this — it just happened because the framework defaults that way.

When LAN access is genuinely needed (testing from a phone on the same network, demoing to someone in the room): say so explicitly in your launch summary so the user knows the exposure exists.

## Polling loops need fail-fast and backoff

A `while true; do <cmd>; sleep N; done` against an external API (kubectl, gcloud, GitHub, k8s, cloud SDKs, anything that needs auth) must:

- **Exit on auth failure.** If the command fails with `Unauthorized`, `ReauthRequired`, `401`, `Token expired`, or similar — stop the loop. Don't retry. The credentials are not going to fix themselves while you're hammering the API. Hammering produces failure-log spam, sometimes millions of files.
- **`sleep ≥ 30s`** for cloud API polls. Per-second polls can generate millions of failure-log files.
- **Run in a named tmux session**, not disowned. So when auth eventually does break, the user can find and kill it.

**Before relaunching a job that looks failed, confirm it stopped.** A status file left by an earlier attempt (an rc file, a log, an empty `.output`) proves nothing about the run you are looking at; check the process (`ps`, the harness's task list) first. Relaunching a job that is still running gives two copies writing the same outputs.

## Tell the user where it lives

When you start a long-running process, your final report must include:
- **PID** (or tmux session name)
- **Bind address + port** — and explicitly note if LAN-exposed
- **How to find it later** — exact `ps` filter, `lsof -nP -iTCP:<port>`, or `tmux attach -t <name>`
- **How to kill it** — `kill <pid>` or `tmux kill-session -t <name>`

Without this, the user cannot clean up after the session ends. Process hygiene is a deliverable, not an afterthought.

## Reap by orphan-status, never by age

A long-lived process is **not** the same as a stale one. The kill signal is a
**dead owner** — parent reparented to PID 1, or the session/tmux/script that
spawned it is gone — not how long it has been running. An 8-day-old process can
be a live daemon doing exactly its job; a 2-minute-old one can already be an
orphan. Before killing anything:

- Confirm the parent: `ps -p <pid> -o ppid=`. PPID 1 (launchd/init) = orphaned.
- If PPID is alive, trace it (`ps -p <ppid> -o command=`) — a live parent means the process is still owned. Leave it.
- Never batch-kill by `etime` or a name match alone. Match each candidate against the expected set first.
- A Claude session started by another session on this machine (tmux) is not PPID 1: its parent is the tmux server. `megavibe sessions --orphans` lists live sessions whose launcher session has ended (from the wrapper's launch records), and the session-start table shows an "Orphaned sessions" row when there are any. Tell the user; ending them is their call. A helper meant to outlive its session (a named tmux session) is launched with `MEGAVIBE_PARENT_SESSION=` (empty), so it is never reported.

This is a real footgun: "kill the oldest `node`/`gemini-mcp`/`python` process"
will happily take down a multi-day session that is still in active use.

## Audit periodically

To find user processes orphaned to launchd (likely candidates for cleanup):

```bash
ps -axo pid,ppid,etime,command | awk '$2==1 && /\/(bash|zsh|sh|python|node|ruby|deno|bun|uvicorn|http\.server|kubectl)\b/ && !/\/System\/|\/usr\/lib|\/Applications\/|com\.apple|\.appex/'
```

Match against the expected set (ssh-agent, gpg-agent / keyboxd, colima/lima/orbstack, intentional tmux sessions, named dev servers). Anything left over is a candidate for `kill`. Walk parent chains with `ps -p <pid> -o ppid=` if you need to know what spawned a polling kubectl or gcloud process — the polling parent is usually a forgotten `/tmp/*.sh` watchdog.

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.