Process Lifecycle
Every process managed by Runix follows a well-defined lifecycle with hook points at each stage.
Lifecycle Stages
Start Sequence
When runix start api is executed:
- Config validation — The
ProcessConfigis validated (required fields, valid runtime, etc.) - Dependency resolution — If
depends_onis set, wait for dependencies to reachrunningstate - Runtime detection — Detect or confirm the runtime adapter (Go, Python, Node.js, Bun, Deno, Ruby, PHP)
pre_starthook — Execute the pre-start command (if configured). If this fails, the process enterserroredstate- Process creation — Build
exec.Cmdwith:Setpgid: truefor process group isolation- Stdout/stderr piped to log files via
PrefixWriter - Environment variables merged from config
- Working directory set from
cwd
- Start execution —
cmd.Start()is called, acquiring a PID post_starthook — Execute the post-start command (if configured)- Health checker — Started if
health_checkis configured - Metrics tracking — PID added to the metrics collector
- Exit monitor — Goroutine started to watch for process exit
Stop Sequence
When runix stop api is executed:
- State transition —
running → stopping(atomic CAS) pre_stophook — Execute the pre-stop command (if configured)- Signal sent — SIGTERM to the process group (
Kill(-pid, SIGTERM)) - Wait — Wait up to
stop_timeoutfor the process to exit - Force kill — If still running after timeout, send SIGKILL
post_stophook — Execute the post-stop command (if configured)- State transition —
stopping → stopped(orcrashedif exit code ≠ 0) - Cleanup — Remove from metrics tracker, stop health checker, clean up cgroups
Restart Sequence
A restart is a stop followed by a start:
pre_stop→ stop →post_stop- Calculate backoff delay (if auto-restart)
- Wait for backoff period
pre_start→ start →post_start
Reload Sequence
A reload is a graceful restart that re-reads configuration:
- Reads the latest config for the process
- Stops the current instance (with hooks)
- Starts a new instance with the updated config (with hooks)
Error Handling
| Scenario | Behavior |
|---|---|
pre_start hook fails | Process enters errored state, does not start |
cmd.Start() fails | Process enters errored state |
post_start hook fails | Logged as warning, process continues running |
pre_stop hook fails | Logged as warning, stop continues |
| Process exits with code 0 | State becomes stopped |
| Process exits with code ≠ 0 | State becomes crashed, restart may be triggered |
Process Group Isolation
All processes run in their own process group (Setpgid: true). This ensures:
SIGTERM/SIGKILLsent to the group (Kill(-pid, signal)) reaches all child processes- Orphaned grandchildren are cleaned up
- No signal leakage between managed processes
What's Next
- Restart Policies — Configurable restart behavior
- Hooks — All 10 lifecycle hook points
- Process State Machine — The 7-state model